missing braces around initializer warning

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    missing braces around initializer warning

    I couldn't find an anser on google...The following code is a tiny made-up example showing the problem I am looking at, at work.

    I have a struct:
    Code:
    struct door_t {
      BOOL color;
      BOOL size;
      char    name[4];
      char    keys[10];
    }
    
    struct door_t knobs = { 
    0, //color
    0, //size
    "", //name[0]
    "", //name[1]
    "", //name[2]
    "", //name[3]
    "" //keys[100]
    };
    The warnings are:
    warning: missing braces around initializer
    warning: (near initialization for 'knobs.name')
    warning: missing initializer
    warning: (near initialization for 'knobs.keys')

    Can someone look at this with me and explain what the missing braces are please?
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    You need a semicolon after the declaration of your struct.
    Hope this helps.
    Edit:
    I see that was probably not what was causing the problem.
    The way you are initializing name is just wrong. You can initialize it to four spaces, like this:
    Code:
    struct door_t knobs = {
    0,
    0,
    "    ",
    "",
    };
    or four nulls:
    Code:
    struct door_t knobs = {
    0,
    0,
    "\0\0\0\0",
    "",
    };
    and you can do it like this:
    Code:
    struct door_t knobs = {
    0,
    0,
    {' ', ' ', ' ', ' '},
    "",
    };
    But you can't initialize it like four separate members of the struct, and you can't initialize it with four strings, and you can't initialize it with empty characters.
    Hope this helps.

    Comment

    • dissectcode
      New Member
      • Jul 2008
      • 66

      #3
      UPDATED CODE:

      Code:
       struct door_t {
         BOOL color;
         BOOL size;
         char    name[4];
         char    keys[10];
       };
        
       struct door_t knobs = { 
       0, //color
       0, //size
       "", //name[0]
       "", //name[1]
       "", //name[2]
       "", //name[3]
       "" //keys[100]
       };

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        Sorry about the incorrect diagnosis of your problem. I hope my edit clears things up.

        Comment

        • dissectcode
          New Member
          • Jul 2008
          • 66

          #5
          Originally posted by boxfish
          Sorry about the incorrect diagnosis of your problem. I hope my edit clears things up.
          Thank you for your help - I didn't know that at all...

          Comment

          • dissectcode
            New Member
            • Jul 2008
            • 66

            #6
            or four nulls:
            Code:
            struct door_t knobs = {
            0,
            0,
            "\0\0\0\0",
            "",
            };
            Hi - I went with four nulls....but I kept getting the "missing braces" warning until I did this:
            Code:
            {"\0\0\0\0"},
            It shut the compiler warning up but is that combination of characters okay?

            Thanks!

            Comment

            • boxfish
              Recognized Expert Contributor
              • Mar 2008
              • 469

              #7
              I'm not sure why the braces shut up the compiler. Try three nulls instead of four, because like any other string, this one will get a null tacked on the end; my bad. Anyway, to fill up your string with nulls, you can just use an empty string, "", and I'm pretty sure that will initialize it to nulls. And if you do it like this:
              {'\0', '\0', '\0', '\0'}
              it won't add the extra null.
              Hope this helps.
              Edit:
              I have a faster keyboard, Banfa. :-p
              Last edited by boxfish; Nov 26 '08, 05:10 PM.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                I suspect the problem is that "\0\0\0\0" is an array of 5 characters (remember the implicit /0 added by the compiler at the end of the string) but name is only 4 characters long.

                Not providing enough data is rarely a problem (the compiler just fills in with 0) but providing too much leaves the compiler wondering what you were talking about.

                If name is a string then simply initialise it to "" an empty string. On the other hand it name is an array of 4 characters then initialise it with {'\0', '\0', '\0', '\0'} and on the other hand if name is actually an array of 4 numbers (in the range of a char) then initialise it with {0, 0, 0, 0}.

                Comment

                • dissectcode
                  New Member
                  • Jul 2008
                  • 66

                  #9
                  Originally posted by Banfa
                  I suspect the problem is that "\0\0\0\0" is an array of 5 characters (remember the implicit /0 added by the compiler at the end of the string) but name is only 4 characters long.

                  If name is a string then simply initialise it to "" an empty string. .

                  OH!!!!!!!!!!!! I just realized that the name array looks like this in the code:
                  Code:
                  char nameVal[MAX_VAL] [40];

                  So it only goes away if I have the brackets:
                  Code:
                   {""},
                  but I don't know much about multi-dimension arrays in C... but it makes the struct initialization behave differently. Why is this?!?!? I would love to know.
                  Thanks!

                  Comment

                  • donbock
                    Recognized Expert Top Contributor
                    • Mar 2008
                    • 2427

                    #10
                    Code:
                    char nameVal[MAX_VAL] [40];
                    
                    /* Is equivalent to ... */
                    typedef char NameString[40];
                    NameString nameVal[MAX_VAL];
                    That is, nameVal is an array of MAX_VAL buffers, each of which is 40 chars long.

                    In your initializer '{""}', the braces enclose the initializers for the MAX_VAL NameStrings. The double quotes enclose the initializer for the first NameString in the array. No initializers are provided for the remaining MAX_VAL-1 NameStrings.

                    Check what happens if you take out the double quotes, so the initializer is simply '{}'. I think that's legal. The result is an empty initializer (which defaults to initial value of all zeroes) that you need as a placeholder if there are initializers for subsequent fields in the structure.

                    I generally avoid multidimensiona l arrays because they almost always engender some level of confusion on the programming team. Worse, the people who are most confused are commonly unaware of it.

                    Comment

                    Working...