the error generate is : Segmentation fault
Myheader.h
#ifndef datest
#define datest
struct date{
char c;
FILE* gestor;
};
typedef struct date dateStruct;
extern dateStruct date_Struct;
void loadLine();
#endif
myMain.c
#include "myheader.h"
...
dateStruct date_Struct;
int main(int argc,char* argv[]){
date_Struct.gestor = fopen(argv[1],"r");
loadLine();
}
MyHeader.c
#include "myheader.h"
void loadLine(){
char* TEXT;
fscanf(date_Struct.gestor, "%s\n", TEXT); //ERROR! why?
...
}
I can’t solve this , i need declare the extern var in myheader.c too?
The segmentation fault might occur because the file wasn’t opened successfully and
date_Struct.gestoris a NULL pointer.Check the return value from
fopen()— always! It may fail, and if you use the null pointer, you get segmentation faults.The other problem is with
TEXT(don’t use all-caps for variables; use lower-case or camel-case and reserve all-caps for macros).The pointer TEXT does not point anywhere; it is not initialized. When you read using it, all hell breaks loose. (The pointer might point anywhere; in the worst case scenario, it points to somewhere valid but unexpected, your
fscanf()‘works’, but you trample over some data and you’ve no idea how it got corrupted. A segmentation fault is fortunate; it tells you that something was clearly wrong.) This is more plausibly your problem.Check the return value from
fscanf()too; it tells you whether it worked as you expected or not. Get used to checking for error returns. It’s always necessary to know how to handle errors, and quite a lot of code is usually devoted to doing so.