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