结构体与共用体大小字节对齐

 1 #include <stdio.h>
 2 //必须整除最宽基本成员
 3 //成员地址-首地址必须可以整除当前成员大小,结构体独有
 4 //不足的填充
 5 
 6 struct MyStruct
 7 {
 8     char str[23];//24
 9     short  db;// 设置,当前最宽 两者取最短的为最宽
10 };
11 
12 struct MyStructX
13 {
14     struct MyStruct x;
15     char   y;//结构体嵌套的情况下,最宽基本成员不局限当前结构体,
16 };
17 
18 void main()
19 {
20     printf("%d", sizeof(struct  MyStruct));
21     system("pause");
22 }
23 
24 /////////////////////////////////////////////////////////////////////////
25 
26 union MyUnion
27 {
28     char str[13];//13 14 
29     int db;     //16
30 
31 };
32 
33 //共用体宽度就是最长的那个,但是必须整除最宽成员
34 
35 void main()
36 {
37     printf("%d", sizeof(union MyUnion));
38     getchar();
39 }

 

posted on 2015-05-19 20:48  Dragon-wuxl  阅读(194)  评论(0)    收藏  举报

导航