This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Does traditional C allow initializing a union?
- To: ghazi at caip dot rutgers dot edu (Kaveh R. Ghazi)
- Subject: Re: Does traditional C allow initializing a union?
- From: Carlo Wood <carlo at runaway dot xs4all dot nl>
- Date: Tue, 13 Oct 1998 16:38:07 +0200 (CEST)
- Cc: egcs at cygnus dot com (egcs at cygnus dot com)
| > typedef union rtunion_def
| > {
| > long rtwint;
| > int rtint;
| > char *rtstr;
| > } rtunion;
| >
| > static rtunion foo = {0};
|
| This works under all ansi compilers I've found, but it fails for
| me on SunOS4 cc and Irix4 'cc -cckr'. Is there another way to
| initialize a union?
The problem with this is that the compiler can't know
which type you are refering to. In this case it would probably
result in the same for each element, but it is still ambiguous.
If you just wish to have all values set to '0', then the fact
that it is a static is sufficient: statics are filled with zero's.
But consider
typedef union rtunion_def
{
double d;
char *p;
char s[3];
} rtunion;
static rtunion foo = { 0 };
Would should that do?
In C++ you could use a constructor, but in C you'll
have to write a seperate intialisation function.
--
Carlo Wood <carlo@runaway.xs4all.nl>