开发者

C structure initialization? [duplicate]

This question already has answers here: How to initialize a struct in accordance with C programming language standards (16 answers) Closed 7 years ago.

How can开发者_如何学Python I initialize a structure if one field in the structure is itself a structure?


You need to use more braces (actually, they're optional, but GCC makes a warning these days). Here's an example:

struct s1 { int a; int b; };
struct s2 { int c; struct s1 s; };

struct s2 my_s2 = { 5, { 6, 3 } };


Nesting of structure

You can initialize a structure if one field in the structure is itself a structure

struct add{
    int house;
    char road;
};
struct emp{
    int phone;
    struct add a;
};

struct emp e = { 123456, 23, "abc"};
printf("%d %d %c",e.phone,e.a.house,e.a.road);


struct A
{
int n;
}

struct B
{
A a;
} b;

You can initialize n by the following statement. Is this what you are looking for.

b.a.n = 10;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜