【发布时间】:2014-02-04 04:21:20
【问题描述】:
我看不出我做错了什么。
void contains(NODE *head, char *user)
{
NODE *this = head;
while(this != NULL)
{
strcpy(temp, this->data);
// printf("%s\n",user);
// printf("%s\n",temp);
printf("%d test\n", strlen(user));
printf("%d node\n", strlen(temp));;
printf("%d\n",strcmp(temp, user));
if(strcmp(this->data, user) == 0){
printf("Found data %s\n", this->data);
}
this = this->next;
}
}
我在 main 中有这个(包含在最后一行调用):
NODE *head;
head = malloc(sizeof(NODE));
bool headNode = true;
char userID[1000];
char userIDsearch[180];
char test[180] = "monkey";
while((fgets(userID,1000,stdin) != NULL)){
// printf("%s\n", userID);
if(headNode == true)
{
head = insert(NULL, userID);
headNode = false; //this is used to insert data to my linked list for the first time
}
else
{
head = insert(head, userID);
}
}
contains(head, test);
如果我输入“monkey”,strcmp 应该给出零,但它给出 1。此外,我在比较的两个字符串上测试并使用了 strlen,出于某种原因,strlen 为 @ 给出长度 + 1 987654327@(我的输入),但strlen 给出了 ~user` 的正确长度(我将其设置为字符串“monkey”)。
【问题讨论】:
-
TrimEnd 您的输入并测试它是否有效:stackoverflow.com/questions/122616/…
标签: c debugging pointers strcmp