我有2个文件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 the host ip to be connected
char *ip = NULL,*port=NULL;//port to connect to
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 i = 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 to connect\n");
exit(0);
}
ptr = search_in_list("Port", NULL);
if ( ptr != NULL)
{
printf("\nPort:%s\n",ptr->val);
port = ptr->val;
//convert the port no to required format.
po = atoi(port);
}
else
{
printf("Can't Find Port to 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 to create\n");
exit(0);
}
return 0;
};
comfunc.c包含
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;
}
}
我的问题是搜索函数返回true值,如果两者的代码
adding_list
和search_list
都在同一个文件中。
但是,如果我在comfunc.c中添加添加列表的代码
在test2.c中ptr = search_in_list("clientip", NULL)
,搜索从不返回true值,它返回NULL。
所以问题就是这种奇怪的行为。为什么以下陈述没有正确比较?
if (strcmp(val,ptr->val) == 0)
found = true;