This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Initialising an union
- To: Alexandre Oliva <oliva at dcc dot unicamp dot br>, egcs-bugs <egcs-bugs at cygnus dot com>
- Subject: Re: Initialising an union
- From: Aurel Balmosan <ab at orga dot com>
- Date: Wed, 19 Aug 1998 09:58:13 +0200
- Organization: ORGA Kartensysteme GmbH
- References: <35D92F35.280D31A1@orga.com> <or4sv9g7cq.fsf@araguaia.dcc.unicamp.br>
Alexandre Oliva wrote:
> Aurel Balmosan <ab@orga.com> writes:
>
> > You will notice that the union X is not fully initialised.
>
> What do you mean by fully initializing a union? You have initialized
> it with only an integer, so only the first integer field will be
> initialized; the remaining fields need not be, because you have not
> initialized them.
As it is defined in ANSI-C a struct where only the first part is initialised
is filled with zero's. As for gcc-2.7.2.3 and older versions the union was
fully initialised. This is not any more with egcs-1.0.3a which we now use.
Either the gcc-2.7.2.3 and older used a not ANSI-C comform initialising
for unions or the egcs-1.0.3a does not follow ANSI-C in this part.
structs are fully initialised to zero when one say ie:
struct c {
int a;
double b;
char c[123];
};
struct d {
char c[234];
char d[231];
int e;
};
struct c Y = { 0 };
struct d Z = { 0 };
This is with gcc-2.7.2.3 and older as well as egcs-1.0.3a. Only the union
is not! And thats my problem and a bug in egcs-1.0.3a
This is defined in the ANSI-C Language Reference A.8.7: Initialisation
I have currently only the german version but here the sentence which
describes that behaviour and applies for struct's as well as for union's:
initializer:
assignment-expression
{ initializer-list }
{ initializer-list, }
initializer-list:
initializer,
initializer-list, initializer
Contains the initializer-list less initializer then the structure component
has the rest values will be set to zero. There must not be more initializer
than values in a structure component.
I think that makes it clear.
Bye,
Aurel.
--
Aurel Balmosan | Department SE-SW
ORGA Kartensysteme GmbH | phone: +49 5254 991 824
An der Kapelle 2 | fax : +49 5254 991 749
33104 Paderborn, Germany | mailto:ab@orga.com