スコープに問題があり、助けが必要です。
main.c、parser.c、parser.h の 2 つのソース ファイルとヘッダー ファイルがあります。
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...
}
問題は、バッファ内の値が保存されていないことです...なぜですか?
構造体を引数として関数に渡していないため、これは「値による呼び出し」の問題ではないことに注意してください。