Codino Codino - 4 months ago 18
C Question

What does struct_type[1] mean?

I face a code that the old guy get the size of struct like this:

sizeof(struct struct_type[1]);


I tested and it does return the size of struct.

And the

sizeof(struct struct_type[2]);


return the double of struct size.

Edit:

struct_type
is not an array, it likes this

struct struct_type {
int a;
int b;
};


What does it really mean ?

Answer

Remember sizeof syntax:

sizeof ( typename );

Here typename is struct struct_type[N] or in more readable form struct struct_type [N] which is an array of N objects of type struct struct_type. As you know array size is the size of one element multiplied by the total number of elements.