Thread: compare strings not working

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    16

    compare strings not working

    I am somewhat new to C programming, but this looks like it should work. I am using MS VS .Net 2002. When I put my mouse over the variable, the strings match up, but when I compare them, the comparison does not equal each other. What I do here is parse a string, then I need to compare it. so if the string is "123 W Main St". It seems to parse ok, but when i come to the "W", I need to compare it with "W" and it does not work.

    Code:
    char * Grab_String(char* string, int length)
    {
    	char *ip;
    	int i = 0;
    
    	ip = (char *) malloc(length*sizeof(char *));
    	while (i < length)
    	{
    		ip[i] = string[i];
    		i = i++;
    	}
    	ip[i] = '\0';
    
    	return ip;
    }
    
    
    char *ParseDelimiter (char *addr_string, char delimiter)
    {
    	char *field;
    	char hold;
    	int seperator;
    	int str_len;
    	int i = 0;
    	int x=0;
    	bool ok=false;
    
    	str_len = strlen(addr_string);
    	while (i <= str_len)
    	{
    		if (addr_string[i] == delimiter)
    			break;
    		i = i++;
    	}	
    
    	field = Grab_String(addr_string, i);
    
    	if ("W"==field)   //////HERE IS THE CODE THAT DOES NOT WORK
    	{
    		ok=true;
    	}
    
    	return field;
    }
    Thanks for helping.

    George

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You must use strcmp to compare strings.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ip = (char *) malloc(length*sizeof(char *));
    1. Don't cast the result of malloc in C - see the FAQ
    2. you use the size of the type, not the size of the pointer
    3. you forgot to count the \0 at the end

    Try
    ip = malloc( (length+1) * sizeof *ip );

    > i = i++;
    This is just wrong
    Use either
    i++;
    or
    i = i + 1;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by hk_mp5kpdw
    You must use strcmp to compare strings.
    strcmp() returns 0 if the strings are equal.

    Code:
    if(strcmp(string1, string2) == 0) {}
    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.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    this is something terribly wrong with this function, but i dont know what it is. If you see, where I commented out the address, if I uncomment it, the function works fine. The other strange thing is that when i am in the dubugger, the variable returned will be the correct one, but the compare strings does not work. If I uncomment, and use the hard code the strings compare fine. Please help, my head hurts from banging it on the desk.

    Code:
    char * left(char * string, int length) 
    { 
    	char *ip;
    	int i = 0;
    
    	ip = (char *) malloc((length*sizeof(char)));
    	while (i < length)
    	{
    		ip[i] = string[i];
    		i++;
    	}
    	ip[i] = '0';
    
    	//ip = "2406 W OCEAN FRONT ST";
    
    	return ip;
    }

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You should pass a previously allocated buffer to your function, because returning local stack data is dangerous and heap data must be free()'ed. You can assume your strings are null-terminated and remove the length parameter probably. Your implementation will end up looking very similar to strcpy (is this what your function intends?), which you will notice also takes a destination and source buffer.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ip = (char *) malloc((length*sizeof(char)));
    Well if you're just going to keep ignoring people, stop asking questions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    ip[i] = '0';
    Maybe you want '\0'?

    Yes, and see Salem's first post.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  2. Working with Strings and Numbers
    By jamez05 in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2005, 12:39 PM
  3. comparing strings using argv
    By eth0 in forum C Programming
    Replies: 2
    Last Post: 09-20-2005, 09:20 AM
  4. working with strings arrays and pointers
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 08:32 PM
  5. c-style string vs. c++-style strings
    By Mbrio in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 12:26 PM