views:

57

answers:

2

Hi, I'm trying to initialise a structure which ends with an array[0] (here, char iedata[0]) for the actual packet payload. If I try to initialise it inline, like this:

struct some_packet pkt = {
   .elem1 = blah, .elem2 = bleh,
   .iedata = {
      1, 2, 3, 4
   }
};

I get a warning from gcc:

warning: (near initialization for ‘pkt.iedata’)

Is there any good way to mark that this is a proper initialisation?

+1  A: 

If you're able to compile in C99 mode, you could try using standard flexible length arrays rather than the zero-length hack: http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

Note that in GCC 3.0 and newer, extra entries in an array initialiser will be discarded (per the documentation linked above).

Andrew Aylett
+1  A: 

As you are using C99 initialization, why not make the member a proper FAM, i.e. char data[];

The only way to create valid struct's with a FAM (or struct hack member) is by dynamically allocating the correct amount of excess storage for the last member so, as the warning suggests, your local initialization isn't valid.

Charles Bailey