Linux C 进阶 —— 零长数组

数据结构中包含的虚拟字段(零长数组)不占字节空间,但是会影响字节对齐,即影响结构体的大小,如

 1 typedef struct 
 2 {
 3   short x;
 4   short y;
 5   short z;
 6 }ST1;
 7  8 typedef struct 
 9 {
10   short x;
11   short y;
12   short z;
13   int  w[0];
14 }ST2;
15 16 int main(void)
17 {
18     ST1 st1;
19     ST2 st2;
20     
21     printf("sizeof(ST1) = %lu B, sizeof(ST2) = %lu B\r\n", 
22            sizeof(ST1), sizeof(ST2));
23            
24     return 0;
25 }

 

输出

sizeof(ST1) = 6 B, sizeof(ST2) = 8 B

 

posted @ 2022-03-21 22:44  云的边界  阅读(113)  评论(0)    收藏  举报