gpt4 book ai didi

c - 在列表中搜索节点既有效又无效

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

这可能是个糟糕的问题,但我完全迷路了。我有这段代码:

struct nodoTemas* search_in_list(char * val, struct nodoTemas **prev,struct nodoTemas *head/*, struct nodoTemas *curr*/)
{
struct nodoTemas *ptr = head;
struct nodoTemas *tmp = NULL;
bool found = false;

printf("\n Searching the list for value [%s] \n",val);

while(ptr != NULL)
{
if(ptr->nombreTema == val)
{
found = true;
break;
}
else
{

tmp = ptr;
ptr = ptr->next;
}
}

if(true == found)
{
if(prev)
*prev = tmp;
return ptr;
}
else
{
return NULL;//si no ha encontrado nada devuelve NULL
}

然后我测试它,在一个特定的文件中测试它,就像这样:

char * var="tema1";
char * var2="tema2";
head=add_to_list(var,true,head,curr);
curr=head;
curr=add_to_list(var2,true,head,curr);
struct nodoTemas* nodoBuscado;
char *temaABuscar="tema4";
nodoBuscado=search_in_list(temaABuscar, NULL,head);

if(nodoBuscado!=NULL)
printf("VALOR DEL NODO %s\n",nodoBuscado->nombreTema);

无论我做什么,它都能完美运行。如果我寻找存在的东西,它会打印它等等。现在在我的主文件上,我得到了 char * 我的服务器从消息中得到了 char *。我认为这失败了,所以我尝试了几件事,这是其中之一:

printf("MATCH %d \n" , strcmp(temaRecibido,head->nombreTema));

结果我得到了 0。所以字符串是一样的。但是在另一个文件中搜索失败。我已经打印了它,我已经检查了它们的 strlen 尺寸,并且它们都匹配。

所以我相信我看错了一面,但我无法理解为什么在一个地方工作的代码在其他地方不起作用。我应该在其他地方寻找错误吗?另外,如果我对带有 null 的字符串和不带 null 的字符串进行 strlen,它们的大小是否相同?男人说它排除了终止字节,但我不确定这一点。

很抱歉,如果帖子缺失,我不确定如何正确呈现它。

最佳答案

你不能比较像

这样的字符串
if(ptr->nombreTema == val)

你应该使用 strcmp

if(strcmp(ptr->nombreTema, val) == 0)

关于c - 在列表中搜索节点既有效又无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29498276/

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