当前位置:网站首页>【C语言】anonymous/unnamed struct&&union
【C语言】anonymous/unnamed struct&&union
2022-06-11 10:58:00 【sidemap】
今天学习muduo代码时,发现在类中如下的方式声明了成员变量:
private:
union
{
struct sockaddr_in addr_;
struct sockaddr_in6 addr6_;
};在类的实现中,直接作为了成员变量使用了addr_ 和 addr6_。
后来经过调查,发现了anonymous/unnamed struct&&union,引用网上一段描述:
Anonymous Union and Structure in C. In C11 standard of C, anonymous Unions and structures were added. Anonymous unions/structures are also known as unnamed unions/structures as they don't have names. Definition is just like that of a normal union just without a name or tag.
翻译:
C中的匿名联合体及结构体。在C11标准中,追加匿名联合体或结构。匿名联合体或结构体由于没有名字也就是无名联合体或结构体。定义就像正常的联合体,只是不带有name或tag。
以前都是声明了一个struct/union为某个类型,然后用该类型创建变量。
typedef union
{
int a;
float b;
} union_type;
union_type var;以上anonymous的方式,在类的对象中可以直接使用 obj.addr_ 或者 obj.addr6_。
写了一个测试程序来说明使用方式,这样看起来更加直观。
#include <stdlib.h>
#include <stdio.h>
struct A1
{
union // 4
{
char c;
short s;
int i;
} __type;
};
struct A2
{
union // 8
{
int i;
long long l;
};
};
struct A3
{
struct // 4+8 => 8+8
{
int st_i;
long long st_ll;
};
};
struct A12 // 4+8 => 8+8
{
union // 4
{
char c;
short s;
int i;
} __type;
union // 8
{
int i;
long long l;
};
};
struct A13 // 4+ 8+8 => 8+ 8+8
{
union // 4
{
char c;
short s;
int i;
} __type;
struct // 4+8 => 8+8
{
int st_i;
long long st_ll;
};
};
struct A23 // 8+ 8+8
{
union // 8
{
int i;
long long l;
};
struct // 4+8 => 8+8
{
int st_i;
long long st_ll;
};
};
struct A // 4 + 8 + 8+8 => 8 + 8 + 8+8
{
union // 4
{
char c;
short s;
int i;
} __type;
union // 8
{
int i;
long long l;
};
struct // 4+8 => 8+8
{
int st_i;
long long st_ll;
};
};
struct B
{
union // 4
{
char c;
short s;
int i;
} __type;
union // 8
{
int i;
long long l;
};
struct // 4+8 => 8+8
{
int st_i;
long long st_ll;
};
struct __NamedStruct
{
int type_st_i;
long long type_st_ll;
};
};
int main()
{
struct A a;
a.__type.i = 4;
printf("a.__type.i=%d\n", a.__type.i);
printf("assign anonymous union.\n");
a.i = 5;
printf("a.__type.i=%d\n", a.__type.i);
printf("a.i=%d\n", a.i);
printf("assign anonymous structure.\n");
a.st_i = 7;
a.st_ll = 15;
printf("a.__type.i=%d\n", a.__type.i);
printf("a.i=%d\n", a.i);
printf("a.st_i=%d\n", a.st_i);
printf("a.st_ll=%lld\n", a.st_ll);
printf("sizezof(struct A)=%lu\n", sizeof(struct A)); // 32
printf("sizezof(struct B)=%lu\n", sizeof(struct B)); // 32
printf("sizezof(struct A1)=%lu\n", sizeof(struct A1)); // 4
printf("sizezof(struct A2)=%lu\n", sizeof(struct A2)); // 8
printf("sizezof(struct A3)=%lu\n", sizeof(struct A3)); // 16
printf("sizezof(struct A12)=%lu\n", sizeof(struct A12)); // 16
printf("sizezof(struct A13)=%lu\n", sizeof(struct A13)); // 24
printf("sizezof(struct A23)=%lu\n", sizeof(struct A23)); // 24
return 0;
}通过以上的赋值及其占用空间大小 ,可以发现对应的对象已经创建。
但是如果在内部声明了一个类型,没有创建该类型的对象,并不占用整个类型的空间(参考struct B与size A的sizeof)。
边栏推荐
- 找到自己的优势,才能干活不累,事半功倍!
- Summary of English thesis reading knowledge
- Introduction to database system -- Chapter 2 -- relational database (2.4 relational algebra)
- Package component series - (I) - slots and dynamic components
- 数字藏品系统app源码
- 使用Labelimg制作VOC数据集或yolo数据集的入门方法
- 杰理之BLE SPP 开启 pin_code 功能【篇】
- 2022年安全月各类活动方案汇报(28页)
- Encrypt and decrypt strings using RSA and Base64
- Team level safety training, new employee induction training education courseware, full content ppt application
猜你喜欢

NFT will change data ownership in the metauniverse

DROID-SLAM: 用于单目双目RGBD相机的深度视觉SLAM

Cloud development MBTI personality type test assistant wechat applet source code

Interpretation of cube technology | past and present life of cube Rendering Design

985 University doctors became popular because of their thanks in classical Chinese! The tutor commented that he not only wrote well in sci

外观模式--在各种套餐中早就用到啦!

Use pydub to modify the bit rate of the wav file, and an error is reported: c:\programdata\anaconda3\lib\site packages\pydub\utils py:170: RuntimeWarning:

杰理之获取 BLE 区分复位跟唤醒【篇】

(key points of software engineering review) Chapter IV overall design exercises

VOC格式数据集转yolo格式数据集的方法
随机推荐
概率论:计算置信区间
Exness: the progress of Russia Ukraine negotiations is limited, and the RBA's decision remains unchanged
数字藏品app小程序公众号系统开发
Jszip get the file of the specified file in the uploaded zip package
Using domestic MCU (national technology n32g031f8s7) to realize pwm+dma control ws2812
95后大厂程序员删库被判刑!只因项目被接手对领导心生不满
杰理之获取 BLE 出现电压检测、ADC 检测不准【篇】
SQL query statement optimization
SAP Spartacus Reference App Structure
封装组件系列-(一)-插槽及动态组件
White screen time, first screen time
Rxjs Observable. Execute logical analysis of pipe passing in multiple operators
What is the best annuity insurance product in 2022?
Where is it safer to open an account for soda ash futures? How much capital is needed to buy soda ash futures?
Campus lost and found applet source code can be used for graduation design
js设置ip屏蔽
错误的导航分类横条代码版本
Remote monitoring project offline log specification
Typeerror: argument of type "Int 'is not Iterable
(key points of software engineering review) Chapter IV overall design exercises