开发者

Initializing a struct containing a file

开发者 https://www.devze.com 2023-03-26 01:07 出处:网络
I defined a simple struct like this: typedef struct SGFile SGFile; struct SGFile{ FILE* sgf_file; }; And a initialization function like this:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消