Empty struct in C
is undefined behaviour (refer C17 spec, section 6.7.2.1
):
If the struct-declaration-list does not contain any named members, either directly or via an anonymous structure or anonymous union, the behavior is undefined.
On my Linux
, I use both gcc (v9.1.0)
and clang (v8.0.0)
to compile following code:
$ cat test.c
#include <stdio.h>
typedef struct A {} A;
int main()
{
printf("%zu\n", sizeof(A));
}
No warnings, and both ouput:
0
Reference:
C empty struct — what does this mean/do?.
A a[10];
printf(“%p == %p”, a + 0, a + 1);
That’s interesting too.
Size 0 of empty pointer so a+0,a+1,….,a+9 all pointing to same address :))
size 0 so all pointing to the same address :))