gpt4 book ai didi

c - 如何比较结构和指针

转载 作者:太空宇宙 更新时间:2023-11-04 06:15:00 24 4
gpt4 key购买 nike

我正在尝试将保存 char 指针的结构的字段与 char 指针进行比较,但比较不起作用。

typedef struct node{
char * word;
struct node * next;
int occurrence;

}No;

           aux = list;
while(aux != NULL){
if(aux->word == token)
{
new_node->occurrence = new_node->occurrence+1;
exist = 0;
}
aux = aux->next;
}

最佳答案

if(aux->word == token)

好吧,你正在比较地址,如果它们相等(这种情况极不可能),它将进入区 block 。

正确的做法是检查内容。 strcmp() 可以帮助您。

strcmp(aux->word, token) == 0

比较他们指向的内容。这在这里很合适。

关于c - 如何比较结构和指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47778194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com