c - If condition and logical comparision not working -


    struct data_struct* search_in_list(char *val, struct data_struct **prev)     {      char *dat=null;     char *dat2=null;     struct data_struct *ptr = head;     struct data_struct *tmp = null;     bool found = false;         printf("\n searching list value [%s] ...found is.%d\n",val,found);      while(ptr != null)     { printf("\n ptr !=null .....searching list value ");     dat = ptr->val;     dat2 = val;      printf("hello world %s.......%s",dat,dat2);      **if (dat == dat2)     printf("hello !!!");**      found = ( val == ptr->val);   printf("the data is%d",found);          if (found)         {  printf("\n ptr val if......searching list value [%s] ",ptr->val);             found = true;             break;         }         else         {  printf("\n else found....searching list value [%s] ",ptr->val);             tmp = ptr;             ptr = ptr->next;         }     }      if(true == found)     {      ptr = ptr->next; printf("\n truefound...searching list value [%s] ",ptr->val);         if(prev)             *prev = tmp;          return ptr;     }     else     { printf("\n searching list value [%s] ",ptr->val);          return null;     } } 

the below condition not working:

if (dat == dat2)  printf("hello !!!"); 

any idea why. if compare ("serverip" == "serverip") works ok..

but if ( ptr->val == val) ..... doesnot work... dunno why. doing wrong... guesss....

because comparing value address:

char* val; //this pointer struct data_struct *ptr = head; //this pointer char *dat=null; //pointer char *dat2=null; //pointer  dat = ptr->val;  //you assign pointer value of 1 element in linked list?! dat2 = val;  //dat2 points same address val 

i thinks it's clear see why not work. solution (other reading on pointers):

*dat = ptr->val;  dat2 = val;  if (*dat == *dat2) //compare 2 values  

just summarize:

char *p; //this pointer char  p //this address p points *p //this value @ address p points 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -