ID Number: Q39910
5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.10 6.00 6.00a
MS-DOS | OS/2
Summary:
In Microsoft C versions 5.0, 5.1, 6.0, 6.0a, 6.0ax, and C/C++ version
7.0, if an attempt is made to initialize the second element of a union
at declaration time (rather than in the code), the following warning
is generated:
C4047:'initialization' - different levels of indirection
This error conforms to the ANSI standard, which assumes that the
initialization is directed at the first element of the union, and
therefore reports that the initializing values are of an incorrect
type. If an attempt is made to initialize both the first and second
element at declaration time, the same warning results.
More Information:
The program below fails with the error C2078 - "too many
initializers." The following initialization fails with C2078 and
C4047. The compiler is expecting the three integers to be initialized
and is finding four character pointers instead.
union TEST test = {ch1,ch2,ch3,ch4};
However, an attempt to initialize the first element of the union is
successful, as follows:
union TEST test = {1,2,3};
If it is necessary to initialize the second element of a union, we
suggest that you change the order of your union members, so that the
element that requires initialization appears as the first element of
the union.
The following program demonstrates this error:
Sample Code
-----------
/* Compile options needed: none
*/
union TEST {
struct{
int a;
int b;
int c;
} one; /* union element #1 */
struct {
char *ptr1;
char *ptr2;
char *ptr3;
char *ptr4;
} two; /* union element #2 */
}u;
char ch1[10], ch2[10], ch3[10],ch4[10] ;
union TEST test ={{1,2,3}, /* initialize element #1 */
{ch1,ch2,ch3,ch4} /* attempt to initialize #2 */
};
void main(void)
{
}
Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00