Reputation: 6075
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? Shouldn't both of them encounter the same error?
Upvotes: 5
Views: 6106
Reputation: 121427
The latter is called a "flexible array member" which is a special case for structures. The last member of a struct is allowed to have no specified 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.
Upvotes: 7
Reputation: 10148
This is not legal in C. Outside of a struct
, your array needs a size when you declare it, as you have no way to set the size later:
int main()
{
unsigned char data[100];
return 0;
}
This is a legal operation in C that allows you to allocate memory for the array when you allocate memory for the struct
. The flexible array member must be the last element in your struct
.
Here is an example, based on the one from GCC's documentation:
struct line
{
int length;
char contents[]; // Flexible array member
};
struct line *myline = malloc(sizeof(struct line) + 100); // 100 bytes for the array
myline->length = 100;
Upvotes: 3