简体   繁体   中英

How to declare a variable of struct that declared inside another struct?

I have a struct that declared as follow:

struct a {
    struct b {
        int d;
    } c;
};

How to declare a variable of b outside a ? In C++ I can use a::bx; . But, in C it required to specifies struct keyword before struct name.

C does not have nested types. You can't write a::xb or anything that ressembles it. If you want to get rid of the struct keyword, that's another problem. Use typedef s. but it won't allow to nest types.

typedef struct b_t {
  int d;
} b;
typedef struct {
  b c;
} a;
b some_b;
a some_a;
int f() {
  some_b.d=42;
  some_a.c=some_b;
  return 0;

}

.

C has a flat layout; when you declare a struct within another struct, the former is just being put into the global namespace.

So, in your example, it is just struct b .

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