简体   繁体   中英

Initializing struct structure member consisting of an int and a char pointer?

Here are the structure declarations:

typedef struct line
{
    int width;
    char *theLine;
} Line;

typedef struct screen
{
    int width;
    int height;
    Line *theScreen;
} Screen;

Here is what I am using to try an initialize the Screen structure:

int main()
{
    Screen b = {20, 40, {40, "-"}};
}

When I compile the above the result is:

warning: braces around scalar initializer [enabled by default]
Screen b = {20, 40, {40, "-"}};
^

What am I doing wrong in the structure initialization? Also, once I am able to compile the code above, how would I access each member of the Line variable withing the struct screen? Any help is greatly appreciated, thank you.

You've defined the 3rd member as a pointer. With Line theScreen; instead of Line *theScreen; , your initialization code would work.

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