Thread: comparing char values

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    128

    comparing char values

    I'm a bit stumpted on this one. First off, I'm writing this to be ported to C, so I'm using char and NOT using string stuff.

    Here is the problem. The code compiles but doesn't run correctly. At the if pch = line, I see that both pch and stringtofind are defined as char * fields. However, when both contain the phrase "get" the code doesn't fall into the if code but jumps down to the else if line.

    Why, if two fields are defined as char * and both have the same value ("get") , aren't they evaulated as the same? My only thought it that it has something to do with pointers.

    Code:
    const char * get_domain(char * cBuffer, char * strtofind)
    {
    	//pull the URL from the header packet
    	//where header is build like
    	//...get /stuff/index.html ...
    
    	int flag = 0;
    	char * pch;
    	//break packet out by spaces as a delimiter
    	pch = strtok (cBuffer," ");
    	while (pch != NULL)
    	{
    		//review each bucket and look for the phrase "get"
    		if (pch == strtofind)
    		{
    			//indicates get is found
    			//flag to grab the next bucket of data which will contain the page location
    			//which could be something like /stuff/index.html 
    			flag = 1;
    		}
    		else if (flag == 1)
    		{
    			return pch;
    		}
    		pch = strtok (NULL, " ");
    	}
    	return "[NONE]";
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Your code is comparing pointers. Use strcmp() instead.
    Kurt

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    Thanks.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    One more interesteing point, if I call the function again, it doesn't perform properly. I have just read the MSDN info that:

    "Each of these functions uses a static variable for parsing the string into tokens. If multiple or simultaneous calls are made to the same function, a high potential for data corruption and inaccurate results exists. Therefore, do not attempt to call the same function simultaneously for different strings and be aware of calling one of these function from within a loop where another routine may be called that uses the same function. However, calling this function simultaneously from multiple threads does not have undesirable effects."

    But if I can't call the function more than once within the same thread??? I think I've read about this problem, here.

    [edit] Ok, I found that subsequent calls would just have NULL sent as the field pointer. Anything else I should know?
    Last edited by FoodDude; 09-29-2005 at 08:53 AM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    you can call strtok() as many times as you want in the same thread. That warning means you can only use strtok() on only one string at a time -- calls can't be nested. This example is WRONG
    Code:
    char *p1 = strtok("once upon a time", " ");
    while(p1 != NULL)
    {
       char* p2 = strtok("there lived three little pigs"," ");
       // blabla
       p1 = strtok(NULL," "); // p1 will now point somewhere inside the string passed to p2
    }
    Last edited by Ancient Dragon; 09-29-2005 at 09:41 AM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Okay, so what is right?

    [edit]See the FAQ.[/edit]
    Last edited by dwks; 09-29-2005 at 01:24 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by dwks
    Okay, so what is right?

    see the op's original post on this thread (and the FAQ you cited) -- it's correct use of strtok().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM