PGC-S-0073-Too many initializers

Hello, I’m a little confused and hope that somebody can help me. I’ve got some structures:

 typedef struct {
         enum BTN_ID btn_id;    
         String      edit_cmd;   
 } CONTEXT_ITEMS;
 
 typedef struct {
     String         edit_mode;
     CONTEXT_ITEMS *selected_item;
     int            nitems;
     CONTEXT_ITEMS  items[];
 } CONTEXT;

then later on I initialize some CONTEXT structures like so:

 static CONTEXT delete_hole_context = {
     E_MODIFY, 0, 2,
     {
         {deleteAreaBtnId,E_DELETE},
         {deleteHoleBtnId,E_DELETE_HOLE}
     }
 };
 static CONTEXT move_context = {
     E_MOVE, 0, 4,
     {
         {translateBtnId,E_TRANSLATE},
         {rotateBtnId,E_ROTATE},
         {cutBtnId,E_CUT},
         {copyBtnId,E_COPY}
     }
 };

I can compile it with gcc and I get no complaints, but when I compile with pgcc I get an error: “PGC-S-0073-Too many initializers for move_context”
It appears that initializing items[] with anything more than 2 CONTEXT_ITEMS structures doesn’t work, but I don’t know why. :(

Any ideas?

Hi Emma,

Sorry for the late reply. I needed to send this to one of our compiler engineers for investigation. He believes that the error here is that we are failing to detect that this code violates the C99 standard. He cites the following:

See the c99 standard, section 6.7.2.1, structure and union specifiers.

"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. With two exceptions, the flexible array member is ignored.

First, the size of the structure …
[specifies what sizeof means for such a structure]
Second, when a . (or ->) …
[specifies what happens when a flexible array member is referenced
with a . or -> operator]"

The example that follows the above makes it clear that attempting to initialize flexible array members is not allowed.

struct s { int n; double d; };
struct ss { int n; double d[1]; };

struct s t1 = { 0 }; // valid
struct s t2 = { 2 }; // valid
struct ss tt = { 1, { 4.2 }}; // valid
struct s t3 = { 1, { 4.2 }}; // invalid: there is nothing for the 4.2 to initialize

He’s currently investigating why gcc allows this code, i.e. is it an extension or a bug. And if it’s an extension, he’ll investigate if we can implement it as well.

  • Mat

I will await your results eagerly.