当前位置: 技术问答>linux和unix
找不到错误~~~~~用字符指针传递变量
来源: 互联网 发布时间:2016-07-28
本文导语: #include #include #include struct filelist { char* filename; struct filelist* next; }; char filename[10]="./test"; static void read_filelist( struct filelist* list_head, char* target); int main() { ...
#include
#include
#include
struct filelist
{
char* filename;
struct filelist* next;
};
char filename[10]="./test";
static void read_filelist( struct filelist* list_head, char* target);
int
main()
{
struct filelist* list_head;
char* dest = (char*)0;
struct filelist* this_node;
read_filelist(list_head, dest);
printf("target_path=%s n", dest);
this_node = list_head ->next;
while(this_node-> next != NULL)
{
printf("filename on the filelist:%sn", this_node->filename);
this_node = this_node->next;
}
printf("targetpath :%sn", dest);
printf("OK!");
}
static void
read_filelist(struct filelist* list_head, char* target)
{
FILE* fp;
char line[10000];
char* cp;
char* cp2;
char* name;
char* value;
struct filelist* this_one;
char* target_path;
this_one = (struct filelist*) malloc(sizeof(struct filelist));
list_head->next = this_one;
fp = fopen( filename, "r" );
if ( fp == (FILE*) 0 )
{
perror( filename );
exit( 1 );
}
while ( fgets( line, sizeof(line), fp ) != (char*) 0 )
{
if ( ( cp = strchr( line, '#' ) ) != (char*) 0 )
*cp = '';
cp = line;
cp += strspn( cp, " t1215" );
printf("line:%sn",line);
printf("line:%d,%d,%dn",line[1],line[2],line[3]);
while ( *cp != 0 )
{
cp2 = cp + strcspn( cp, " t1215" );
while ( *cp2 == ' ' || *cp2 == 't' || *cp2 == '12' || *cp2 == '15' )
*cp2++ = '';
name = cp;
if( (value = strchr( name, ':' ) ) == NULL)
break;
*value = '';
value ++;
if(strcmp(name, "filelist") == 0)
{
while( value != (char*)0 && strcmp(value, "endfilelist"))
{
cp2 = strchr (value, ';');
*cp2 = '';
this_one->next =(struct filelist*)malloc(sizeof(struct filelist));
this_one->filename =(char*)malloc(strlen(value) + 1);
strcpy(this_one->filename,value);
printf("value:%sn",value);
value = cp2 + 1;
this_one->next->next = NULL;
this_one = this_one->next;
}
cp = value + sizeof("endfilelist");
}
else if(strcmp(name, "target") == 0)
{
cp2 = strchr (value,';');
*cp2 = '';
target_path = (char*) malloc(strlen(value) + 1);
if(target_path != NULL)
{
strcpy(target_path, value);
printf("target_path=%sn",target_path);
}
else
printf("malloc failed!n");
cp = cp2 + sizeof("endtarget") + 1;
}
}
}
target = target_path;
}
我哭了,单步调试,函数最后执行的时候target_path的值还是正确的;可一出函数就编程NULL了。。。为什么啊?哪位大虾帮着看看,谢谢
test的内容
filelist:ems;e2m;e2u;endfilelist
filelist:ems;e2m;e2u;endfilelist
target:testdir;endtarget
|
最简单的C语言知识,传值还是传引用,你这里传值想把值带出来怎么行呢?
|
struct filelist
{
char* filename;
struct filelist* next;
};
struct filelist* list_head;
struct filelist* this_node;
this_node = list_head ->next;
-----------------
struct filelist
{
char* filename;
struct filelist* next;
}; 里面根本没有next 成员啊
{
char* filename;
struct filelist* next;
};
struct filelist* list_head;
struct filelist* this_node;
this_node = list_head ->next;
-----------------
struct filelist
{
char* filename;
struct filelist* next;
}; 里面根本没有next 成员啊
|
不好意思 看错了
在看看
在看看