简体   繁体   中英

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 (0x000000).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM