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?

AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
  • 1
    https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html – zerkms Mar 10 '18 at 10:06
  • You mean to say "_unspecified array size_". – machine_1 Mar 10 '18 at 10:17
  • (off-topic: can you undelete https://stackoverflow.com/questions/49396346/why-are-segfaults-called-faults-if-they-are-not-recoverable? 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) – Peter Cordes Mar 21 '18 at 05:00
  • @PeterCordes It has been undeleted – AlanSTACK Mar 21 '18 at 05:04

2 Answers2

7

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.

P.P
  • 117,907
  • 20
  • 175
  • 238
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;
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
  • 1
    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. – Eric Postpischil Mar 10 '18 at 10:43
  • @EricPostpischil Thanks for the additional information! I edited my answer accordingly. – Ronan Boiteau Mar 10 '18 at 10:47