c - Search function not working between files -
i have 2 files test2.c , comfunc.c
test2.c conatins
#include "common.h" #include <stdio.h> #include <stdlib.h> int main() { printf("hello"); int n, i,po; char *process=null;//stores host ip connected char *ip = null,*port=null;//port connect char *pch=null; ip = malloc(50*sizeof(char)); pch = malloc(50 *sizeof(char)); port = malloc(50 * sizeof(char)); process = malloc(5 * sizeof(char)); //int n; char str[650]; //int = 0, ret = 0; struct data_struct *ptr = null; ptr = openfile("server.cfg", o_rdonly); ptr = search_in_list("clientip", null); if ( ptr != null) { printf("\nclientip:%s\n",ptr->val); ip = ptr->val; } else { printf("can't find clientip connect\n"); exit(0); } ptr = search_in_list("port", null); if ( ptr != null) { printf("\nport:%s\n",ptr->val); port = ptr->val; //convert port no required format. po = atoi(port); } else { printf("can't find port connect\n"); exit(0); } ptr = search_in_list("process", null); if ( ptr != null) { printf("\nprocess:%s\n",ptr->val); process = ptr->val; } else { printf("can't find process create\n"); exit(0); } return 0; };
and comfunc.c contains
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; while(ptr != null) { printf("values of val:=%s......ptr->val:=%s\n",val,ptr->val); if (strcmp(val,ptr->val) == 0) found = true; if (found) { found = true; break; } else { tmp = ptr; ptr = ptr->next; } } if(true == found) { ptr = ptr->next; if(prev) *prev = tmp; return ptr; } else { return null; } }
my problem search functions returns true values, if code of both adding_list
, search_list
both in same file.
but, if put code of adding list in comfunc.c , ptr = search_in_list("clientip", null)
in test2.c, search never returns true values, returns null.
so question why such strange behavior. why below statement not comparing properly?
if (strcmp(val,ptr->val) == 0) found = true;
Comments
Post a Comment