简体   繁体   English

初始化由int和char指针组成的struct结构成员吗?

[英]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: 这是我用来尝试初始化Screen结构的内容:

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? 另外,一旦我能够编译上面的代码,如何在结构屏幕上访问Line变量的每个成员? Any help is greatly appreciated, thank you. 非常感谢您的任何帮助,谢谢。

You've defined the 3rd member as a pointer. 您已将第3个成员定义为指针。 With Line theScreen; Line theScreen; instead of Line *theScreen; 代替Line *theScreen; , your initialization code would work. ,您的初始化代码将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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