473,383 Members | 1,896 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,383 software developers and data experts.

warning: missing braces around initializer

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 {
union {
int a;
};
union {
int b;
};
};

static struct entry en[] = { { 12, 13 } };
int main() {
(void)en;
};

I compile it on linux with following command:
g++ -Wall -g -O0 -o /tmp/p2 p2.cpp

regards,
Pawel
Nov 2 '06 #1
12 13402
Pawel wrote:
May be:
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 {
union u_a{
int a;
};
union u_a{
int b;
};
};

static struct entry en[] = { { 12, 13 } };
int main() {
(void)en;
};

I compile it on linux with following command:
g++ -Wall -g -O0 -o /tmp/p2 p2.cpp

regards,
Pawel
Nov 2 '06 #2
Pawel wrote:
Hallo group members

Could You tell hw to write this code in order not to get warning from subject:
struct entry {
union {
int a;
};
union {
int b;
};
};

static struct entry en[] = { { 12, 13 } };
To statically initialize an object of type entry you have to statically
initialize each of its fields. Since each of those fields is in turn an
aggregate type, the compiler is warning you that you haven't used braces
to mark the initializers for those fields. In this case it's okay, since
there's only one initializer needed for each of those fields. So the
first union is initialzied with 12 and the second with 13. If you change
those unions to structs (adding names, of course) the initialization
would still work the same way. But if you then added a field to the
first one, the 12 and 13 would be used to initialize its two fields, and
the second struct would be initialized to 0.

So, to get rid of the warning, put braces around the initializer for
each of the aggregate members of entry:

static struct entry en[] = { { {12}, {13} } };

--

-- Pete

Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." For more information about this book, see
www.petebecker.com/tr1book.
Nov 2 '06 #3

Pawel wrote:
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 {
union {
int a;
};
union {
int b;
};
};

static struct entry en[] = { { 12, 13 } };
static entry en[] = { { {12},{13} } };
int main() {
(void)en;
};
remove that last semicolon. Note that the last statement has no effect.
Which is a good thing since:

void n; is illegal

and en actually decays to a pointer. So you could argue:
int main()
{
void* pvoid = static_cast<void*>(&en);
std::cout << "pvoid = " << pvoid << std::endl;
}
>
I compile it on linux with following command:
g++ -Wall -g -O0 -o /tmp/p2 p2.cpp

regards,
Pawel
Nov 2 '06 #4
Salt_Peter wrote:
Pawel wrote:
>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 {
union {
int a;
};
union {
int b;
};
};

static struct entry en[] = { { 12, 13 } };

static entry en[] = { { {12},{13} } };
>int main() {
(void)en;
};

remove that last semicolon. Note that the last statement has no effect.
Which is a good thing since:

void n; is illegal
How is that relevant? (void)en is not a declaration or definition; it is
a cast converting n to void.

--
Clark S. Cox III
cl*******@gmail.com
Nov 2 '06 #5

Clark S. Cox III wrote:
Salt_Peter wrote:
Pawel wrote:
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 {
union {
int a;
};
union {
int b;
};
};

static struct entry en[] = { { 12, 13 } };
static entry en[] = { { {12},{13} } };
int main() {
(void)en;
};
remove that last semicolon. Note that the last statement has no effect.
Which is a good thing since:

void n; is illegal

How is that relevant? (void)en is not a declaration or definition; it is
a cast converting n to void.
No its not, it doesn't cast if you don't have an lvalue to cast to. A
cast does not modify the casted entity itself. In no way is en's type
changed above.
Only if an lvalue where assigned would the cast apply. Which is
impossible because there is no such thing as a void. Hence my point.
>
--
Clark S. Cox III
cl*******@gmail.com
Nov 3 '06 #6
Salt_Peter wrote:
Clark S. Cox III wrote:
>Salt_Peter wrote:
>>Pawel wrote:
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 {
union {
int a;
};
union {
int b;
};
};

static struct entry en[] = { { 12, 13 } };
static entry en[] = { { {12},{13} } };

int main() {
(void)en;
};
remove that last semicolon. Note that the last statement has no effect.
Which is a good thing since:

void n; is illegal
How is that relevant? (void)en is not a declaration or definition; it is
a cast converting n to void.

No its not, it doesn't cast if you don't have an lvalue to cast to. A
cast does not modify the casted entity itself. In no way is en's type
changed above.
Nobody said anything about modifying en. The expression:

(void)foo

is an expression that evaluates foo (including any side effects), and
then casts the value of the expression to void.

"(void)foo;" has no relationship to "void foo;"

any more than:

"(void*)foo;" does to: "void *foo;"
Only if an lvalue where assigned would the cast apply. Which is
impossible because there is no such thing as a void. Hence my point.
a cast has nothing to do with an lvalue, "(int)42" contains a cast and
not a single lvalue, as does "(void)en".

Take, as an example:

#include <iostream>

struct Foo
{
operator int() const { std::cout << "operator int\n"; return 123; }
} foo;

int main()
{
(int)foo;
return 0;
}

Are you claiming that there is no cast?

--
Clark S. Cox III
cl*******@gmail.com
Nov 3 '06 #7
"Salt_Peter" <pj*****@yahoo.comwrites:
Pawel wrote:
>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 {
union {
int a;
};
union {
int b;
};
};

static struct entry en[] = { { 12, 13 } };

static entry en[] = { { {12},{13} } };
>int main() {
(void)en;
};

remove that last semicolon. Note that the last statement has no effect.
Which is a good thing since:

void n; is illegal
I cast it to omit warning of unused variable, but maybe this is wrong
way to do that.

regards
Nov 3 '06 #8
Pete Becker <pe********@acm.orgwrites:
To statically initialize an object of type entry you have to
statically initialize each of its fields. Since each of those fields
is in turn an aggregate type, the compiler is warning you that you
haven't used braces to mark the initializers for those fields. In this
case it's okay, since there's only one initializer needed for each of
those fields. So the first union is initialzied with 12 and the second
with 13. If you change those unions to structs (adding names, of
course) the initialization would still work the same way. But if you
then added a field to the first one, the 12 and 13 would be used to
initialize its two fields, and the second struct would be initialized
to 0.

So, to get rid of the warning, put braces around the initializer for
each of the aggregate members of entry:

static struct entry en[] = { { {12}, {13} } };
No warning now:)
Thank You
Nov 3 '06 #9

Clark S. Cox III wrote:
Salt_Peter wrote:
Clark S. Cox III wrote:
Salt_Peter wrote:
Pawel wrote:
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 {
union {
int a;
};
union {
int b;
};
};

static struct entry en[] = { { 12, 13 } };
static entry en[] = { { {12},{13} } };

int main() {
(void)en;
};
remove that last semicolon. Note that the last statement has no effect.
Which is a good thing since:

void n; is illegal
How is that relevant? (void)en is not a declaration or definition; it is
a cast converting n to void.
No its not, it doesn't cast if you don't have an lvalue to cast to. A
cast does not modify the casted entity itself. In no way is en's type
changed above.

Nobody said anything about modifying en. The expression:

(void)foo

is an expression that evaluates foo (including any side effects), and
then casts the value of the expression to void.
agreed
>
"(void)foo;" has no relationship to "void foo;"
i disagree. without the lhv the cast never happens.
>
any more than:

"(void*)foo;" does to: "void *foo;"
Only if an lvalue where assigned would the cast apply. Which is
impossible because there is no such thing as a void. Hence my point.

a cast has nothing to do with an lvalue, "(int)42" contains a cast and
not a single lvalue, as does "(void)en".

Take, as an example:

#include <iostream>

struct Foo
{
operator int() const { std::cout << "operator int\n"; return 123; }
} foo;

int main()
{
(int)foo;
return 0;
}

Are you claiming that there is no cast?
None whatsoever.
however, the following would be a cast:

int main()
{
// sizeof(foo) = 1
const int& n = static_cast<int>(foo); // or ... = (int)foo;
// sizeof(foo) is still 1
}

Do you see now why (void)en doesn't make sense? Thats my point.
And by the way, my statement was wrong, instead of "lvalue" i should
have said variable - or even parameter.

Nov 3 '06 #10
Salt_Peter wrote:
Clark S. Cox III wrote:
>Salt_Peter wrote:
>>Clark S. Cox III wrote:
Salt_Peter wrote:
Pawel wrote:
>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 {
> union {
> int a;
> };
> union {
> int b;
> };
>};
>>
>static struct entry en[] = { { 12, 13 } };
static entry en[] = { { {12},{13} } };
>
>int main() {
> (void)en;
>};
remove that last semicolon. Note that the last statement has no effect.
Which is a good thing since:
>
void n; is illegal
How is that relevant? (void)en is not a declaration or definition; it is
a cast converting n to void.
No its not, it doesn't cast if you don't have an lvalue to cast to. A
cast does not modify the casted entity itself. In no way is en's type
changed above.
Nobody said anything about modifying en. The expression:

(void)foo

is an expression that evaluates foo (including any side effects), and
then casts the value of the expression to void.

agreed
>"(void)foo;" has no relationship to "void foo;"

i disagree. without the lhv the cast never happens.
>any more than:

"(void*)foo;" does to: "void *foo;"
>>Only if an lvalue where assigned would the cast apply. Which is
impossible because there is no such thing as a void. Hence my point.
a cast has nothing to do with an lvalue, "(int)42" contains a cast and
not a single lvalue, as does "(void)en".

Take, as an example:

#include <iostream>

struct Foo
{
operator int() const { std::cout << "operator int\n"; return 123; }
} foo;

int main()
{
(int)foo;
return 0;
}

Are you claiming that there is no cast?

None whatsoever.
If there is no cast, then why does it call (Foo::operator int)?
(Foo::operator int) is called because a value of type Foo is being
converted to a value of type int; It is being so converted because it
requested that it be converted by casting it.
however, the following would be a cast:

int main()
{
// sizeof(foo) = 1
const int& n = static_cast<int>(foo); // or ... = (int)foo;
// sizeof(foo) is still 1
}
How is sizeof(foo) relevant? It never changes; in either situation.

int main()
{
// sizeof(foo) = 1
static_cast<int>(foo); // Line A
const int &n = static_cast<int>(foo); // Line B
// sizeof(foo) is still 1
}

Except for the fact that the value of the cast expression is discarded
in "Line A", "Line A" and "Line B" are semantically identical. They both
contain casts that convert foo to an int.
Do you see now why (void)en doesn't make sense? Thats my point.
And by the way, my statement was wrong, instead of "lvalue" i should
have said variable - or even parameter.
The result of a cast doesn't need to be used for it to be considered a
cast. You can safely ignore the value of *any* expression and it does
not change the meaning of the expression itself.
--
Clark S. Cox III
cl*******@gmail.com
Nov 3 '06 #11

Clark S. Cox III wrote in message <12*************@corp.supernews.com>...
>Salt_Peter wrote:
>Clark S. Cox III wrote:
>>Salt_Peter wrote:
Clark S. Cox III wrote:
Salt_Peter wrote:
>Pawel wrote:
>>Hallo group members
>>>
>>Could You tell hw to write this code in order not to get warning from
subject:
>>>>>>#include <iostream>
>>using namespace std;
Uh, any chance you guys could trim a little dead-post once in awhile?

Nov 3 '06 #12
Pawel wrote:
"Salt_Peter" <pj*****@yahoo.comwrites:
>Pawel wrote:
<snip>
>>static struct entry en[] = { { 12, 13 } };
static entry en[] = { { {12},{13} } };
>>int main() {
(void)en;
};
remove that last semicolon. Note that the last statement has no effect.
Which is a good thing since:

void n; is illegal

I cast it to omit warning of unused variable, but maybe this is wrong
way to do that.

regards
This is fine; he is wrong.

Tom
Nov 4 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

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...
9
by: Ape Ricket | last post by:
Hi. During my program's set-up phase where it reads in the arguments it was invoked with, I programmed this: if (strcmp(argv,"-G") ==0) { geom_scaling = ON; if (i < argc-1)...
4
by: bingfeng | last post by:
I have some codes generated by perl, in which initialize some huge struct,such as PARA TOS_network_spantree_set_0_para_0 = { "vlan", emNUM, NULL, "", "configuration on a designated vlan",...
1
by: Andrew Maclean | last post by:
Please look at the sample code below first. I understand the problem but I don't know how to get around it. I can see that I am passing a pointer to an unconsructed object to another...
14
by: Andrew Bullock | last post by:
I thought the two bits of code should be the same? They aren't evaluating the same for me .... Can someone tell me why please? Thanks Andrew
4
by: silversurfer2025 | last post by:
Hello everyone, I am using a struct inside another class, which can be used through an empty constructor or another one. The definition looks like this: struct Entry{ int index; //line 38...
13
by: Sam Steingold | last post by:
the following program does not compile with g++ 4.1.1: ==================================================== // $Id$ // $Source$ #include <stdint.h> #include <stdio.h> typedef struct {...
6
by: Daniel Rudy | last post by:
Hello Group. Please consider the following code: /* this table is used in the wipedevice routine */ static const struct wipe_t { uchar wte; /* wipe table entry */ } wipetable = {...
9
by: dissectcode | last post by:
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: struct door_t { BOOL color; BOOL size; ...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.