记得之前在哪里看到了如下形式的结构体和联合体的命名方式;
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中的变量。
十分好用。
C语言中的匿名联合与结构体:内存优化与访问便捷
本文介绍了C语言中匿名联合和结构体的使用,这种特性允许在不创建独立变量的情况下嵌套结构体或联合体,从而节省内存。通过匿名联合,可以直接通过结构体访问其内部的变量,提高了代码的简洁性和访问效率。C11标准中正式引入了这一特性,使得在结构体中嵌套匿名联合成为一种强大的工具。
1496

被折叠的 条评论
为什么被折叠?



