Initializing a struct containing a file
I defined a simple struct like this:
typedef struct SGFile SGFile;
struct SGFile{
FILE* sgf_file;
};
And a initialization function like this:
SGFile* sgf_file_new (void){
GFile* sgf = {NULL};
return sgf;
}
In my main, I try to initialize my function like this:
SGFile* sgf = sgf_file_new();
It's obviously not what I want to do, because sgf still points to NULL (0x0开发者_运维问答00000).
My question is: what's the best practice to initialize a struct containing a FILE element?
Your help is very much appreciated.
I think in your function sgf_file_new
, you wanted to do this:
SGFile* sgf_file_new (void)
{
GFile* sgf = (GFile*)malloc(sizeof(GFile));
return sgf;
}
In fact, you only initialized the pointer with the value 0.
精彩评论