简体   繁体   中英

Strings in C - print is the same strcmp says they are not

Hi guys I am having a problem with my server client project. I run my proxy server with the command ./server www.google.com so i have argv[1]=www.google.com . After this I run my client and I send to the server the value GET www.google.com . Now from server side I use strtok_r(buffer," ",&string1) so I have buffer=GET and string1=www.google.com (I'm sure for these valuse cause I print them). The problem is when I use

if((strcmp(string1,argv[1]) == 0))
        {       
            printf(" SAME VALUES \n");
        }

I don't get the printf so these 2 variables don't have the same value. Any ideas ?

In strtok_r(buffer," ",&string1) , string1 isn't the pointer to the token found. You appear to be comparing string1 but the function return value is the token pointer. So the code should be

char * tok = strtok_r(buffer," ",&string1);
if ((strcmp (tok,argv[1]) == 0))
    {       
        printf(" SAME VALUES \n");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM