Search for item [List]

3

I want to search an element of a list by a name (vector of characters). I get the name (espm) in function and childbirth for the search. The problem is that the function always tells me "There is no doctor with this specialty" even when the name (espm) exists in my lita ( aux->esp==espm ).

Note: I have already tried to search for a number ( int or float ) with this same function and worked. But when I try to search with a string it goes wrong. WHY ???

void pesquisa_med(char* espm){

MEDICO *aux;
int flag=0;
aux = primeirom->mprox;
while(aux!=NULL){
    if(aux->esp == espm){
        printf("%s %s", aux->mnome, aux->mapelido);
        flag=1;
        aux=NULL;
    }
    else
        aux=aux->mprox;
}
if(!flag)
    printf("Nao existe medico com essa especialidade.");

}
    
asked by anonymous 23.08.2016 / 02:46

1 answer

2

No such strings are compared in C. Since strings are character arrays, the == operator only compares memory addresses, and only returns true when the two variables point to the same address.

The right thing is to use the strcmp function (or its case-insensitive version), defined in the string.h library. Its usage looks like this:

if (strcmp(aux->esp, espm) == 0){ // ...
    
23.08.2016 / 02:52