# C语言struct和union结合使用，空间最小

struct StUserData
{
unsigned userdata1 : 1;		// 0--		1--
unsigned userdata2 : 2;		// 0--    1--    2--   3--
unsigned userdata3 : 3;
unsigned userdata4 : 4;

unsigned normaldata1 : 1;
unsigned normaldata2 : 2;
unsigned normaldata3 : 3;
unsigned normaldata4 : 4;

unsigned otherdata1 : 3;
unsigned otherdata2 : 3;
unsigned otherdata3 : 3;
unsigned otherdata4 : 3;
};

union UnUserData
{
StUserData info;
UINT32 data;
};

int _tmain(int argc, _TCHAR* argv[])
{
UnUserData data;
memset(&(data.info), 0, sizeof(data.info));

data.info.userdata1 = 1;
data.info.userdata2 = 3;
data.info.userdata3 = 5;
data.info.userdata4 = 7;

data.info.normaldata1 = 0;
data.info.normaldata2 = 3;
data.info.normaldata3 = 5;
data.info.normaldata4 = 7;

data.info.otherdata1 = 0;
data.info.otherdata2 = 2;
data.info.otherdata3 = 4;
data.info.otherdata4 = 6;

UnUserData data2;
data2.data = data.data;
return 0;
}

#### c语言中的结构（struct）和联合（union）简介

2016-07-04 22:22:32

#### C语言中struct与union的区别

2016-11-29 16:53:30

#### C语言中，struct与union的区别

2015-07-28 09:29:26

#### C语言中的struct、union、enum、Typedef

2015-03-16 15:34:36

#### c语言的union和struct

2017-05-17 21:14:01

#### C_C语言的enum、struct、union的使用详解

2014-11-10 20:01:01

#### Struct 和 Union区别

2012-09-07 12:04:33

#### C语言之struct和union分析

2018-03-27 23:23:11

#### C语言--struct和union

2017-09-09 19:55:29

#### struct和union在内存所占空间

2015-04-27 15:31:42