Search code examples
cgccstructinitializationbraces

gcc complaints about braces in initialiser (nested structs)


In chasing down a very opaque bug, I have been trying to compile with no warnings or errors. This part of my code worked fine, but gcc complains about the braces--it says there are braces missing and extra braces. I usually initialise a bit more sloppily but here I'm being as pedantic as possible with braces for each logical level of inclusion. The only struct I really care about initialising is the last one, the Ccfg. I thought I'd build up to it gradually as it contains nested other structs, but apparently even the ones preceding it are mis-initialized according to gcc.

Here's the code:

#define max_nodes 24

struct Cseg
        {
        int begin;
        int arc;
        int end;
        };

struct Node
        {
        struct Cseg exit[4];
        };

struct Core
        {
        int num_circles;
        int num_nodes;
        struct Node node[max_nodes];
        };

struct Ccfg
        {
        struct Core core;
        int dummy1;
        int dummy2;
        };

int main(void)
{
        struct Cseg A = {0,1,2};

        struct Node B =
                {
                  {0,1,2}, {1,3,0}, {2,-1,3}, {0,-2,1}
                };

        struct Node C =
                {
                  {0,1,2}, {1,3,0}
                };

        struct Core D =
                {4, 4,
                        {
                          {   {0,1,2}, {1,3,0}, {2,-1,3}, {0,-2,1}   },
                          {   {1,3,0}, {2,1,0}, {3,-2,1}, {2,-1,0}   },
                          {   {3,1,2}, {0,1,2}, {1,-3,0}, {2,-3,1}   }
                        }
                };


        struct Ccfg E =
                {
                  {2, 2,
                        {
                          { {0,1,1}, {0,2,1} },
                          { {1,2,0}, {1,1,0} }
                        }
                  }
                };

        return 0;
}

Some of the initialisations are incomplete; that is deliberate. My real ccfg struct has many more fields but I've simplified it for this post. If someone could let me know what I'm doing wrong I'd appreciate it a lot. Thanks!

EDIT: in my working code, the initialiser for struct Ccfg E omits the innermost braces, and works fine (but gcc still warns me about it). I added them into this test because they seemed logically appropriate, but they actually generate an error--which I don't understand.


Solution

  • You are missing braces in some places. Specifically, if you have an array of structs, the entire array needs to be brace-wrapped; you were just wrapping each struct entry. I just added braces as needed and it works fine now. http://ideone.com/fork/HqxB9R

    #define max_nodes 24
    
    struct Cseg
            {
            int begin;
            int arc;
            int end;
            };
    
    struct Node
            {
            struct Cseg ex[4];
            };
    
    struct Core
            {
            int num_circles;
            int num_nodes;
            struct Node node[max_nodes];
            };
    
    struct Ccfg
            {
            struct Core core;
            int dummy1;
            int dummy2;
            };
    
    int main(void)
    {
            struct Cseg A = {0,1,2};
    
            struct Node B =
                    {
                      { {0,1,2}, {1,3,0}, {2,-1,3}, {0,-2,1} }
                    };
    
            struct Node C =
                    {
                      { {0,1,2}, {1,3,0} }
                    };
    
            struct Core D =
                    {4, 4,
                            {
                              { {   {0,1,2}, {1,3,0}, {2,-1,3}, {0,-2,1}   } },
                              { {   {1,3,0}, {2,1,0}, {3,-2,1}, {2,-1,0}   } },
                              { {   {3,1,2}, {0,1,2}, {1,-3,0}, {2,-3,1}   } }
                            }
                    };
    
    
            struct Ccfg E =
                    {
                      {2, 2,
                            {
                              { { {0,1,1}, {0,2,1} } },
                              { { {1,2,0}, {1,1,0} } }
                            }
                      }
                    };
    
            return 0;
    }