This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Does traditional C allow initializing a union?


|  > 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>



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]