Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What purpose does the declaration "int t[0];" serve?

Tags:

c

c99

What is the purpose of the following declaration?

struct test
{
     int field1;
     int field2[0];
};
like image 852
elhadi dp ıpɐɥןǝ Avatar asked Dec 27 '22 23:12

elhadi dp ıpɐɥןǝ


1 Answers

That's simply a 0 length array. According to http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html:

Zero-length arrays are allowed in GNU C. They are very useful as the last element of a structure which is really a header for a variable-length object:

 struct line {
   int length;
   char contents[0];
 };

 struct line *thisline = (struct line *)
 malloc (sizeof (struct line) + this_length);
 thisline->length = this_length;
like image 137
Tudor Avatar answered Jan 12 '23 15:01

Tudor