1

In the C, syntax of flexible array member like this:

struct s 
{ 
    int n; 
    double d[];  // flexible array member
};

And, Zero size array illegal in C.

If I declare array like this:

struct s 
{ 
    double d[0];  // Zero size array
};

GCC give warning:

warning: ISO C forbids zero-size array 'd' [-Wpedantic]

So, I'm going to my main question.

I saw following code here.

struct squashfs_xattr_entry {
    __le16          type;
    __le16          size;
    char            data[0];
};

In C zero-size array illegal.

Then,

  • What is the purpose of this data[0] declaration in struct?
  • What does data[0] do here?
msc
  • 33,420
  • 29
  • 119
  • 214

1 Answers1

4

Prior to C99 (the ISO standard version of C released in 1999) the only way to implement a flexible array member was if the compiler supported it as an extension. GCC supported it by using a static length of 0, hence foo buffer[0].

C99 made it legal, but they decided to prescribe the syntax foo buffer[] instead of retaining GCC's [0] version.

GCC still supports buffer[0] for compatibility with code written before C99.

This is explained in GCC's documentation: https://gcc.gnu.org/onlinedocs/gcc-4.4.4/gcc/Zero-Length.html (emphasis mine):

Zero-length arrays are allowed in GNU C

Note that "GNU C" (the GCC implementation of C) has its own extensions on-top of ISO C.

Dai
  • 141,631
  • 28
  • 261
  • 374