0

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 struct ccfg 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.

5
  • The tabbed in braces make my eyes bleed! Commented Feb 20, 2015 at 20:13
  • 'exit' is a C system call. suggest using some unique name Commented Feb 20, 2015 at 20:25
  • what are the actual error messages? Commented Feb 20, 2015 at 20:30
  • Good point about "exit". It hasn't caused any problems, probably because it's inside the name-space for that particular type so doesn't conflict with the system call... but I agree it's wise to change it. Commented Feb 20, 2015 at 21:50
  • "Some of the initialisations are incomplete" nope, not really! There are no "incomplete initializations" in C. An object is either uninitialized or fully initialized. If only parts of the object are specified within the initialization the rest is initialized with 0 of the right kind, recursively if needed. Commented Nov 6 at 10:40

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.