#c strcmp() 没有正常工作
我想比较两个 char* 所以我用 strcmp 来做, 看,在调试模式下,strcmp 中的两个 char* 获得相同的值,但仍然如此 不会返回 0,并且它会跳过 if() 条件而不是输入它:
这里来自调试器的图片: http://img405.imageshack.us/img405/5218/111fi.jpg
Company FindCompany(CompanyL pcompanyList, int companyIdentityDigit)
{
Company companyFound;
char *psearchWord;
psearchWord = (char*)malloc(10*sizeof(char));
switch(companyIdentityDigit) {
case 0:
strcpy(psearchWord , "Pelephone");
break;
case 2:
strcpy(psearchWord , "Cellcom");
break;
case 4:
strcpy(psearchWord , "Orange");
break;
}
while(pcompanyList->next != NULL) {
if(strcmp(pcompanyList->thisCompany->pcompany , psearchWord) == 0) {
free(psearchWord);
return pcompanyList->thisCompany;
}
pcompanyList = pcompanyList->next;
}
free(psearchWord);
return NULL;
}
这是为什么?
I want to compare between two char* so i do it with strcmp,
look, in debugging mode, both char* in strcmp gets the same value and still it
wont return 0, and it jumps over the if() condition instead of entering it:
here pic from the debugger:
http://img405.imageshack.us/img405/5218/111fi.jpg
Company FindCompany(CompanyL pcompanyList, int companyIdentityDigit)
{
Company companyFound;
char *psearchWord;
psearchWord = (char*)malloc(10*sizeof(char));
switch(companyIdentityDigit) {
case 0:
strcpy(psearchWord , "Pelephone");
break;
case 2:
strcpy(psearchWord , "Cellcom");
break;
case 4:
strcpy(psearchWord , "Orange");
break;
}
while(pcompanyList->next != NULL) {
if(strcmp(pcompanyList->thisCompany->pcompany , psearchWord) == 0) {
free(psearchWord);
return pcompanyList->thisCompany;
}
pcompanyList = pcompanyList->next;
}
free(psearchWord);
return NULL;
}
why is it??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试一个简单的 for 循环来一次打印出 pcompanyList->thisCompany->pcompany 中的字符:
您可以执行此操作或检查每个字符串的长度以确保没有未显示的隐藏字符检查字符串时在调试器中启动。
Try a simple for loop to print out the characters in pcompanyList->thisCompany->pcompany one at a time:
You can do this or check the length of each string to make sure there aren't hidden characters that aren't showing up in the debugger when you check the strings.