记得之前在哪里看到了如下形式的结构体和联合体的命名方式;
union
{
char alpha;
int num;
};
struct
{
char alpha;
int num;
};
因为写代码的时候用到了,在外网上找了点资料,把这个点记下来;
Anonymous unions/structures are also known as unnamed unions/structures as they don’t have names. Since there is no names, direct objects(or variables) of them are not created and we use them in nested structure or unions.
Definition is just like that of a normal union just without a name or tag.
这个特性是为了在struct 和union嵌套的时候访问变量更加容易,而在C 11当中添加的。
struct Scope {
// Anonymous union
union {
char alpha;
int num;
};
};
如上,使用union节省了内存,而且在访问的时候直接通过struct 可以指向union中的变量。
十分好用。