Reputation: 673
This might be a bad question but I'm completly lost. I have this code:
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
}
}
And I test it, in a specific file to test it, like this:
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);
And it works perfectly fine no matter what I do. If I look for something that exists it prints it and so on. Now on my main file I get the char * my server gets the char * from a message. I thought this failed so I tried several things, this being one of them:
printf("MATCH %d \n" , strcmp(temaRecibido,head->nombreTema));
And I get a 0 as a result. So the strings are the same. But the search fails, in this other file. I've printed it, I've checked for their strlen sizes, and it all matches.
So I believe I'm looking at the wrong side but I cannot understand why code that is working in one place does not work in other. Should I look for the mistake somewhere else? Also, if I do strlen of a string WITH null and one without null, are they the same size? man says it excludes the terminating byte but I am unsure about this.
I'm sorry if the post is lacking I wasn't sure how to properly present it.
Upvotes: 0
Views: 47
Reputation: 126
im not sure if you can compare strings in C that way... if you search for the string "tema1" instead a pointer to "tema1" what is the result? also, check this http://www.wikihow.com/Compare-Two-Strings-in-C-Programming.
Upvotes: 1
Reputation: 584
You cannot compare strings like
if(ptr->nombreTema == val)
You should use strcmp
if(strcmp(ptr->nombreTema, val) == 0)
Upvotes: 1