LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2004, 05:36 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
Problem when using the strchr function.


Problem when using the strchr function.
The code below result in a null value despite the fact that
a colon is in the string. This is incorrect
ip_address = strchr(ip_address_string, ':');

The code below result in an 'r' value because the letter
r is in the string. This is correct
ip_address = strchr(ip_address_string, 'r');


===============================
root:/home# ./string_1
ip_address = (null)
===============================

Code:
#include <stdio.h>  
#include <stdlib.h>   
#include <string.h> 

main ()

{
  char ip_address_string[14] = "addr:10.4.0.1";
  char *ip_address;

  ip_address = strchr(ip_address_string, ':');
  printf ("ip_address = %s\n", ip_address);

}
 
Old 05-07-2004, 05:47 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I don't know...it works okay for me:

Code:
itsme:~/C$ cat strchr.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main ()
{
  char ip_address_string[14] = "addr:10.4.0.1";
  char *ip_address;

  ip_address = strchr(ip_address_string, ':');
  printf ("ip_address = %s\n", ip_address);

}
itsme:~/C$ ./strchr
ip_address = :10.4.0.1
itsme:~/C$
 
Old 05-07-2004, 06:02 PM   #3
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
reply

Hi itsme86. Thank you for your help.
I used the function s = strtok(ip_address_string, ":")
and then I used the function
ip_address = strchr(ip_address_string, ':') and that is why
it gives me a null value.

The function strtok kept a hidden static pointer.
When I called the strchr function afterward, the pointer points to null.
If I commented out the code s = strtok(ip_address_string, ":")
it gives a correct answer when I run the code

ip_address = strchr(ip_address_string, ':')

===================================
Code:
#include <stdio.h>              
#include <stdlib.h>       
#include <string.h>   

main ()

{
  char ip_address_string[14] = "addr:10.4.0.1";

  char *s;
  char *save_pointer;
  char *ip_address;


  s = strtok_r(ip_address_string, "addr:",  &save_pointer);
  printf ("using strtok_r function.  s = %s\n", s);


  s = strtok(ip_address_string, ":");
  printf ("using strtok   function.  s = %s\n", s); 


  ip_address = strchr(ip_address_string, ':');
  /* ip_address = (strchr(ip_address_string_2, '1') + 1); */
  printf ("ip_address = %s\n", ip_address);

}
 
Old 08-21-2006, 08:46 AM   #4
pblack15
LQ Newbie
 
Registered: Jun 2006
Posts: 2

Rep: Reputation: 0
Problem when using the strchr function.

I am having a similar problem with strchr with SUSE Linux 10.1. The following code compiles and works fine on MS-Dos and SCO Unix 5.06 but generates a Segmentation fault on SUSE Linux 10.1.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
static char buffer[80];
void converter();

fprintf(stdout,"Try entering 90-45-9\n");
fgets(buffer,79,stdin);
converter(buffer);
fprintf(stdout,"Try entering 90.775\n");
fgets(buffer,79,stdin);
converter(buffer);
return 0;
}
void converter(char *buffer)
{
char *p, *q;
double d, e, f;

d = atof(buffer);
p = strchr(buffer,'-');
p++;
if(*p == '-')
{
e = atof(p);
q = strchr(p,'-');
if(*q == '-')
{
q++;
f = atof(q);
}
}
fprintf(stdout,"%f %f %f\n",d, e, f);
}
 
Old 08-21-2006, 09:05 AM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
welcome to linux questions. in the future please use code tags as they preserve the formatting and make the code alot easier to read.

as for your segfault, it might have something to do with you asking for a .

> fprintf(stdout,"Try entering 90.775\n");

and then looking for a -

> strchr(buffer,'-');

then you continue through converter() without verifying that p is valid. if you were to add this block after p = strchr(buffer, '-'); in converter.. you should see where you are going wrong.
Code:
if(!p)
{
    printf("did not find char in string, bailing out\n");
    return;
}
 
Old 08-22-2006, 08:36 AM   #6
pblack15
LQ Newbie
 
Registered: Jun 2006
Posts: 2

Rep: Reputation: 0
Talking Problem when using the strchr function

Follow up on my earlier post:

Use p = strchr(buffer,'-');
if(p == NULL)

NOT p = strchr(buffer,'-');
if(*p == NULL)
 
Old 08-22-2006, 10:39 AM   #7
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by pblack15
Follow up on my earlier post:

Use p = strchr(buffer,'-');
if(p == NULL)

NOT p = strchr(buffer,'-');
if(*p == NULL)
im sorry maybe its just because i cant read minds very well, but i have not idea what you are talking about.. care to elaborate?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
HI, I have a problem with a function in C grupoapunte Programming 3 06-13-2005 04:53 AM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM
Problem with included function in C Claus Programming 12 10-22-2003 08:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration