473,322 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. struct door_t {
  2.   BOOL color;
  3.   BOOL size;
  4.   char    name[4];
  5.   char    keys[10];
  6. }
  7.  
  8. struct door_t knobs = { 
  9. 0, //color
  10. 0, //size
  11. "", //name[0]
  12. "", //name[1]
  13. "", //name[2]
  14. "", //name[3]
  15. "" //keys[100]
  16. };
  17.  
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?
Nov 25 '08 #1
9 21940
boxfish
469 Expert 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. struct door_t knobs = {
  2. 0,
  3. 0,
  4. "    ",
  5. "",
  6. };
or four nulls:
Expand|Select|Wrap|Line Numbers
  1. struct door_t knobs = {
  2. 0,
  3. 0,
  4. "\0\0\0\0",
  5. "",
  6. };
and you can do it like this:
Expand|Select|Wrap|Line Numbers
  1. struct door_t knobs = {
  2. 0,
  3. 0,
  4. {' ', ' ', ' ', ' '},
  5. "",
  6. };
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.
Nov 26 '08 #2
UPDATED CODE:

Expand|Select|Wrap|Line Numbers
  1.  struct door_t {
  2.    BOOL color;
  3.    BOOL size;
  4.    char    name[4];
  5.    char    keys[10];
  6.  };
  7.  
  8.  struct door_t knobs = { 
  9.  0, //color
  10.  0, //size
  11.  "", //name[0]
  12.  "", //name[1]
  13.  "", //name[2]
  14.  "", //name[3]
  15.  "" //keys[100]
  16.  };
  17.  
Nov 26 '08 #3
boxfish
469 Expert 256MB
Sorry about the incorrect diagnosis of your problem. I hope my edit clears things up.
Nov 26 '08 #4
@boxfish
Thank you for your help - I didn't know that at all...
Nov 26 '08 #5
or four nulls:
Expand|Select|Wrap|Line Numbers
  1. struct door_t knobs = {
  2. 0,
  3. 0,
  4. "\0\0\0\0",
  5. "",
  6. };
Hi - I went with four nulls....but I kept getting the "missing braces" warning until I did this:
Expand|Select|Wrap|Line Numbers
  1. {"\0\0\0\0"},
  2.  
It shut the compiler warning up but is that combination of characters okay?

Thanks!
Nov 26 '08 #6
boxfish
469 Expert 256MB
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
Nov 26 '08 #7
Banfa
9,065 Expert Mod 8TB
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}.
Nov 26 '08 #8
@Banfa

OH!!!!!!!!!!!! I just realized that the name array looks like this in the code:
Expand|Select|Wrap|Line Numbers
  1. char nameVal[MAX_VAL] [40];
  2.  

So it only goes away if I have the brackets:
Expand|Select|Wrap|Line Numbers
  1.  {""},
  2.  
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!
Nov 26 '08 #9
donbock
2,426 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. char nameVal[MAX_VAL] [40];
  2.  
  3. /* Is equivalent to ... */
  4. typedef char NameString[40];
  5. NameString nameVal[MAX_VAL];
  6.  
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 multidimensional 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.
Nov 26 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

97
by: Kjetil Torgrim Homme | last post by:
often when re-factoring code, I need to change the indent level of some chunk of code. due to the lack of an end marker, my Emacs has to use heuristics when re-indenting, and this will...
4
by: Bill Dee | last post by:
I use XML comments around most of my class libraries public property and methods, for example: /// <summary> /// blah blah /// </summary> However I have a few dozen public constants as well...
12
by: Pawel | last post by:
Hallo group members Could You tell hw to write this code in order not to get warning from subject: #include <iostream> using namespace std; using namespace __gnu_cxx; struct entry {
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.