Article ID: 39910 - Last Review: July 5, 2005 - Revision: 2.1 PRB: Initializing Non-Primary Union Element Produces ErrorsThis article was previously published under Q39910 On This PageSYMPTOMS
Attempting to initialize any element of a union except the first may
generate any combination of the following compiler messages:
error C2078: too many initializers warning C4047: 'initialization' - different levels of indirection
-or-
warning C4047: 'initializing' : 'type1' differs in levels of indirection
from 'type2' warning C4133: 'initializing' : incompatible types - from 'type1' to 'type2' CAUSE
The compiler issues these messages by design. According to the ANSI
standard, initialization of a union must be directed to the first element.
An attempt to initialize some other union member may be interpreted by the
compiler as a syntactically incorrect initialization of the first element.
RESOLUTION
If it is necessary to initialize a non-primary element of a union, change
the order of your union members, so that the element that requires
initialization appears as the first element of the union.
MORE INFORMATION
Example1.c, in the sample code below, fails compilation with one C2078
error and three C4047 warnings. The error is generated because only one
union element may be initialized (the first) and by the time the compiler
processes the "{ch1,ch2,ch3,ch4}" initialization, it has already filled
element one. The warnings are generated because the compiler expects to be
initializing "union TEST tsty" with integer values, but it is finding char*
(character pointers) instead.
Example2.c compiles but produces a C4133 warning. The compiler expects to initialize "union ONION anONION" with an integer pointer but finds a float pointer instead. Although, this situation is not serious enough to prohibit compilation, it does indicate that there may be a problem. Sample CodeREFERENCES
For additional information, please see the following article in the
Microsoft Knowledge Base:
47693
(http://support.microsoft.com/kb/47693/EN-US/
)
Initializing Unions Initializes First Member of the Union
APPLIES TO
| Article Translations
|

Back to the top
