当前位置:网站首页>[C language] anonymous/unnamed struct & Union
[C language] anonymous/unnamed struct & Union
2022-06-11 11:27:00 【sidemap】
Learning today muduo Code , It is found that member variables are declared in the following way in the class :
private:
union
{
struct sockaddr_in addr_;
struct sockaddr_in6 addr6_;
};In the implementation of the class , It is directly used as a member variable addr_ and addr6_.
After investigation , Found out anonymous/unnamed struct&&union, Quote an online description :
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.
translate :
C Anonymous associations and structures in . stay C11 In the standard , Add an anonymous union or structure . Anonymous consortiums or structures have no name, that is, anonymous consortiums or structures . Definition is like a normal Union , Just without name or tag.
In the past, it was declared that struct/union For a type , Then create variables with this type .
typedef union
{
int a;
float b;
} union_type;
union_type var;above anonymous The way , It can be used directly in class objects obj.addr_ perhaps obj.addr6_.
Write a test program to explain how to use , It looks more intuitive .
#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;
}Through the above assignment and its occupied space , You can find that the corresponding object has been created .
But if you declare a type internally , No object of this type was created , Does not occupy the entire type of space ( Reference resources struct B And size A Of sizeof).
边栏推荐
- 在WordPress媒体库中创建文件夹
- Adapter mode -- can you talk well?
- my.cnf中 [mysql]与[mysqld] 的区别 引起的binlog启动失败的问题
- 使用Labelimg制作VOC数据集或yolo数据集的入门方法
- msf cs openssl流量加密
- 使用国产MCU(国民技术 N32G031F8S7) 实现 PWM+DMA 控制 WS2812
- AcWing 1353. Ski resort design (greedy)
- CAP理论听起来很高大上,其实很简单
- MSF CS OpenSSL traffic encryption
- Test cos HTML cache static cache plug-in
猜你喜欢

Adapter mode -- can you talk well?

Introduction to database system -- Chapter 2 -- relational database (2.4 relational algebra)

Use yolov3 to train yourself to make datasets and get started quickly

不做伪工作者

企业微信小程序避坑指南,欢迎补充。。。

收货地址列表展示【项目 商城】
![Définir l'adresse de réception par défaut [Centre commercial du projet]](/img/eb/2864b124b66d01849315a367948ed4.png)
Définir l'adresse de réception par défaut [Centre commercial du projet]

Command mode - attack, secret weapon

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:

msf cs openssl流量加密
随机推荐
木瓜移动CFO刘凡 释放数字时代女性创新力量
Etcd的运行时重配置
数据库系统概论 ---- 第二章 -- 关系数据库(2.1~2.3)(重要知识点)
使用Labelimg制作VOC数据集或yolo数据集的入门方法
拆分数据---水平拆分和纵向拆分
在毕设中学习03
JS interview questions - arrow function, find and filter some and every
发布WordPress数据库缓存插件:DB Cache Reloaded 3.1
想做钢铁侠?听说很多大佬都是用它入门的
Source code construction of digital collection system
Set the default receiving address [project mall]
CAP理论听起来很高大上,其实很简单
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:
数字藏品app小程序公众号系统开发
数字藏品系统源码搭建
WordPress站内链接修改插件:Velvet Blues Update URLs
JS prototype. The find () method has no effect on the object array. It is urgent...
Split data - horizontal split and vertical split
NFT digital collection system development and construction process
AI security and Privacy Forum issue 11 - stable learning: finding common ground between causal reasoning and machine learning