当前位置:网站首页>[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支持注册用户上传自定义头像功能
- Planck plan 2022 Huawei software elite challenge is coming!
- 小 P 周刊 Vol.08
- Digital collection system app source code
- Don't be a fake worker
- 在畢設中學習03
- MSF CS OpenSSL traffic encryption
- Exness: the progress of Russia Ukraine negotiations is limited, and the RBA's decision remains unchanged
- AcWing 1353. Ski resort design (greedy)
- Only when you find your own advantages can you work tirelessly and get twice the result with half the effort!
猜你喜欢

js面试题---箭头函数,find和filter some和every

Use yolov5 to train your own data set and get started quickly

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

Characteristics and classification of creation mode (single case, factory)

Typeerror: argument of type "Int 'is not Iterable

IIHS tsp+ annual safety list released: 7 EVs were selected, and there are common problems in pedestrian AEB

Shi Yigong: I was not interested in research until I graduated from my doctor's degree! I'm confused about the future, and I don't know what to do in the future

使用国产MCU(国民技术 N32G031F8S7) 实现 PWM+DMA 控制 WS2812

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

命令模式--进攻,秘密武器
随机推荐
msf cs openssl流量加密
测试cos-html-cache静态缓存插件
想做钢铁侠?听说很多大佬都是用它入门的
MySQL optimized learning diary 10 - locking mechanism
Planck plan 2022 Huawei software elite challenge is coming!
Tu ne peux pas être libre sans richesse?
在畢設中學習03
使用国产MCU(国民技术 N32G031F8S7) 实现 PWM+DMA 控制 WS2812
Interpretation of cube technology | past and present life of cube Rendering Design
让WordPress支持注册用户上传自定义头像功能
Introduction to thread pool: ThreadPoolExecutor
Bark – 自己给自己的 iPhone 发推送提醒 – 最简单的推送提醒服务,开源免费
AcWing 1353. Ski resort design (greedy)
WP Super Cache静态缓存插件简明使用教程
Characteristics and classification of creation mode (single case, factory)
Inventory of the 9 most famous work task management software at home and abroad
IIHS tsp+ annual safety list released: 7 EVs were selected, and there are common problems in pedestrian AEB
Droid-slam: depth vision slam for monocular and binocular rgbd cameras
迭代器模式--沙场秋点兵
数据库系统概论 ---- 第二章 -- 关系数据库(2.1~2.3)(重要知识点)