我的范围有问题,需要帮助。
我有 2 个源文件和一个头文件:main.c、parser.c 和 parser.h
在 parser.h 中:
struct buffer{
char member1[30];
char member2[20];
char member3[20];
char member4[20];
}buf;
void parse(char* line);
在 parser.c 中:
void parse(char* line){
clear_buf(); //I clear my current buffer before running this function
char temp[30];
// .... some code which copies from my line into my temporary buffer (temp)
// .... some code which decides which of my buffers I want to copy this to
strcpy(buf.member1,temp);
//Check the addresses- the struct buf is the same, the member is not:
//printf("buffer INSIDE function %p\n",&buf.member1);
//printf("STRUCT BUF, INSIDE function %p\n",&buf);
// at THIS point, when checking, buf.member1 does have the correct data copied into it
}
在 main.c 中:
while(fgets(line,100,fp)!=NULL){
/*parse the line into our internal buffer*/
parse(line);
//check addresses in main- buf.member1 is different, but the struct buf is the same
//printf("STRUCT BUF, in main %p\n",&buf);
//printf("buffer in main %p\n",&buf.member1);
//rest of code...
}
问题是我的缓冲区中的值没有被保留......为什么不呢?
请注意,这不是“按值调用”问题,因为我没有将结构作为参数传递给任何函数。