#c strcmp() 没有正常工作

发布于 2024-10-04 16:36:40 字数 1065 浏览 1 评论 0原文

我想比较两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╭⌒浅淡时光〆 2024-10-11 16:36:40

尝试一个简单的 for 循环来一次打印出 pcompanyList->thisCompany->pcompany 中的字符:

for (int x = 0; x < strlen(pcompanyList->thisCompany->pcompany); x++)
    printf("%c ", pcompanyList->thisCompany->pcompany[x]);

您可以执行此操作或检查每个字符串的长度以确保没有未显示的隐藏字符检查字符串时在调试器中启动。

Try a simple for loop to print out the characters in pcompanyList->thisCompany->pcompany one at a time:

for (int x = 0; x < strlen(pcompanyList->thisCompany->pcompany); x++)
    printf("%c ", pcompanyList->thisCompany->pcompany[x]);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文