5

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?

asked Mar 10, 2018 at 10:03
4
  • 1
    gcc.gnu.org/onlinedocs/gcc/Zero-Length.html Commented Mar 10, 2018 at 10:06
  • You mean to say "unspecified array size". Commented Mar 10, 2018 at 10:17
  • (off-topic: can you undelete stackoverflow.com/questions/49396346/…? I wrote an answer for it which I think is interesting, but you deleted it a few minutes before I was done. Commenting here because there's nowhere to comment on a deleted post) Commented Mar 21, 2018 at 5:00
  • @PeterCordes It has been undeleted Commented Mar 21, 2018 at 5:04

2 Answers 2

7

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.

answered Mar 10, 2018 at 10:10
Sign up to request clarification or add additional context in comments.

Comments

3

In your first example, you have defined a regular array without giving its size.

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;
}

In your second example, you have defined a flexible array member.

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;
answered Mar 10, 2018 at 10:15

2 Comments

Technically, it is not a zero-length array. The proper name is flexible array member. GCC allowed zero-length arrays (using [0]) as a language extension before the C standard added flexible array members (using []). While the compiler treats it as a size of zero when reporting the size of the structure, it is generally expected to have a non-zero size when storage is allocated for the structure.
@EricPostpischil Thanks for the additional information! I edited my answer accordingly.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.