Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is undefined size array in struct allowed?

Tags:

c

When I write the following code, I get the expected error error: array size missing in 'data'.

int main()
{
    unsigned char data[];

    return 0;
}

However, when I run the same code but wrap the offending line inside a struct, there are no errors.

typedef struct credit_card_s
{
  unsigned char is_valid;

  unsigned char data[];

} credit_card_t;

Can anyone explain to me why this is allowed?

like image 270
AlanSTACK Avatar asked Jan 03 '23 16:01

AlanSTACK


1 Answers

The later is called a "flexible array member" which is special case for structures. The last member of a struct is allowed to have no size.

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

Also see example 20.

The former is a normal array and it's not allowed to have zero size. See 6.7.6.2 Array declarators.

If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero. In other words, the language standard says so.

like image 51
P.P Avatar answered Jan 05 '23 18:01

P.P