1 #include <stdio.h>
2
3
4 struct test {
5 char c;
6 int i;
7 double d;
8 void *p;
9 int a[0];
10 };
11
12 int main (void) {
13 struct test t;
14 printf("size of struct is: %d\n", sizeof(t));
15 return 0;
16 }
輸出:結構尺寸
size of struct is: 20
爲什麼int a[0]
沒有考慮?
我嘗試:
1 #include <stdio.h>
2
3
4 struct test {
5 int a[0];
6 };
7
8 int main (void) {
9 struct test t;
10 printf("size of struct is: %d\n", sizeof(t));
11 return 0;
12 }
和輸出:
size of struct is: 0
a[0]
是結構的一個成員。那結構的大小怎麼沒有考慮呢?
你期望'int a [0]'有多大? – glglgl
只是一個瘋狂的猜測 - 一個[0]是零元素的數組,聲音邏輯大小爲零。 –
int a [0]將零整數存儲在內存中。 – Nocturno