String comparison troubles: mary != mary
For some reason my method of comparing string is wrong or there is something wrong with my strings. My output looks like this:
Code:
parent(bob,mary)You want to check if bob is a parent of mary
Looking for childnode
mary == mary
peter == mary
john == mary
and here is the snippet of the relevant code:
Code:
char *arg1,*arg2;
char *tmp;
strtok(buffer,"(");
tmp=strtok(NULL,",");
arg1 = strdup(tmp);
tmp=strtok(NULL,")");
arg2=strdup(tmp);
parent(hp,arg1,arg2);
Code:
int parent(struct family *hp, char* parent, char* offspring){
struct family *cp = hp;
while(cp->child){
cp=cp->child;
printf("%s == %s\n",cp->name,offspring);
if(strcmp(cp->child->name,offspring) == 0){
printf("found the child\n");
break;
}
}
if(cp->parent1->name == parent)
return 1;
else if(cp->parent2)
if(cp->parent2->name == parent)
return 1;
return 0;
}
Even after printing out mary == mary, the program does not execute the "found the child" printf and continues traversing the tree. I think the problem is with the strings.