From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <1119e18f49257ed2c95b4499278e0b94.axp-list@archive.invalid> X-Archive-Original-Message-ID: Date: Tue, 4 Jun 1996 10:46:23 +0300 (EET DST) From: Linus Torvalds Subject: Re: gcc question In-Reply-To: <199606032221.PAA15714@koala.azstarnet.com> On Mon, 3 Jun 1996, David Mosberger-Tang wrote: > > Please note that it is _not possible_ to initialize unions in ANSI C. Actually, it's possible (umm - raincheck on the standard here, I'm too lazy to look it up, but I'm fairly certain). The standard allows you to initialize unions, but only the _first_ entry in a union. > And there are good reasons why this is so: mgetty fails to compile > under Linux/Alpha because there a pointer is 64 bits wide whereas an > int is only 32 bits wide. The patch below makes use of a GNU C > extension. With the patch, mgetty will compile as long as GCC is > used. However, it would be better to initialize the variants at > runtime, since that would make the program more portable. Another possibility is to put the largest entity first (the pointer), and then always initialize even the integers as pointers (rather than initializing the pointers as integers). That will then work on little-endian machines like the alpha (and will work on any big-endian machines where the sizes match, of course, taking care of most of them)... A third possibility is to use "long" instead of "int" in the union, and casts to (long) instead of (int) to initialize the entries. That's probably just about as portable as this is going to get. I agree with David that the right way to do this is to do the init at run-time, but that is often fraught with other problems. Maybe an updated C standard will make the gcc extension a standard feature some day. Using a gcc-specific feature may be the best thing to do in this case, but if somebody wants to use DEC cc without hacking the code to death, I'd just suggest using the "long" approach. Linus