Is someone familiar with C language specification and can explain the idea of structures in C? I always thought that array in C is always a pointer (i.e. 4 bytes of data in 32bit systems) but in context of structures there is a slight difference.
When you define structure with a constant size array (e.g. int ns[5]
) the size of the structure is 5*sizeof(int) = 20
bytes. I predicted it to be just 4 bytes. When you pass the structures by the value you create a whole new structure along with data in that array.
From the other hand when you have a structure with a dynamic size array (e.g. int *ns) the size is just sizeof(int *) = 4
bytes as predicted.
You can see and try the source code here: http://ideone.com/8gQhZT where i print:
printf("Size of static = %d and size of the dynamic = %d", sizeof(a), sizeof(b));
for static and dynamic structure.
I would like to know if it is consistent with C language specification and why is solved that way.