In the program
#include<stdio.h>
struct t {
char a[5];
char b[];
} temp;
int main(){
temp.b[0] = 'c';
temp.b[1] = 'b';
temp.b[2] = '\0';
printf("Size of struct = %lu\n", sizeof(temp));
printf("String is %s\n", temp.b);
printf("Address of temp = %p\n", &temp);
printf("Address of array a = %p\n", &(temp.a));
printf("Address of b = %p\n", &(temp.b));
}
with output
Size of struct = 5
String is cb
Address of temp = 0x601035
Address of array a = 0x601035
Address of b = 0x60103a
In this program, how exactly is array b being allocated? How long is it? Is this some undefined behavior, which is only succeeding in the dummy program as I am not doing anything else. Running into gdb, I can access some memory locations initialized as zero, which makes me suspect that it is allocating some memory.
I do have an api that requires me to format one element of struct as int a[][SIZE], and I am confused about that.
Also, why is sizeof not taking into account at least something from array b. I am not sure if it is taking it as an array or pointer.