简体   繁体   English

C语言中的字符串-打印与strcmp相同,即它们不是

[英]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 . 我使用./server www.google.com命令运行代理服务器,所以我有argv[1]=www.google.com After this I run my client and I send to the server the value GET www.google.com . 之后,我运行客户端,并将值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). 现在,从服务器端,我使用strtok_r(buffer," ",&string1)所以我有buffer=GETstring1=www.google.com (我确定有这些值,因为我打印了它们)。 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. 我没有得到printf,所以这两个变量没有相同的值。 Any ideas ? 有任何想法吗 ?

In strtok_r(buffer," ",&string1) , string1 isn't the pointer to the token found. strtok_r(buffer," ",&string1)string1不是指向找到的令牌的指针。 You appear to be comparing string1 but the function return value is the token pointer. 您似乎正在比较string1但函数返回值是令牌指针。 So the code should be 所以代码应该是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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