2017-02-12 55 views
0

当我比较两个节点内的数据时,它表示它们不相等,但它们打印的是相同的信息。相同的数据但两个节点内部不相等

while(currentUserTry != NULL && currentPassword != NULL) { 

    if(currentUserTry->color != currentPassword->color){ 
     printf("user %s - %lu\n", currentUserTry->color, strlen(currentUserTry->color)); 
     printf("pass %s - %lu\n", currentPassword->color, strlen(currentPassword->color)); 
    } 

    currentUserTry = currentUserTry->next; 
    currentPassword = currentPassword->next; 
} 

打印:
用户AZ - 2
通AZ - 含
用户虚拟机 - 2
通VM - 2

+0

顺便说一下,让我们知道是否提出的答案工作通过勾选接受,答案。如果什么都行不通,请在下面的回答中评论什么不起作用以及应该改变什么。 – nikaltipar

回答

0

在这个if语句必须使用标准的C函数strcmp

if(strcmp(currentUserTry->color, currentPassword->color) != 0){ 
1

比较两个字符串时使用strcmp

while(currentUserTry != NULL && currentPassword != NULL) { 

    if(strcmp(currentUserTry->color, currentPassword->color)){ 
     printf("user %s - %lu\n", currentUserTry->color, strlen(currentUserTry->color)); 
     printf("pass %s - %lu\n", currentPassword->color, strlen(currentPassword->color)); 
    } 

    currentUserTry = currentUserTry->next; 
    currentPassword = currentPassword->next; 
} 

C库函数int的strcmp(常量字符* STR1,常量字符* STR2)比较指向的字符串,由STR1的字符串由STR2指向。

该函数的返回值是如下:

如果返回值< 0则表明STR1小于STR2。

如果返回值> 0,则表示str2小于str1。

如果返回值= 0,则表示str1等于str2。

相关问题