INFO: Initializing Unions Initializes First Member of the Union (47693)
The information in this article applies to:
- Microsoft C for MS-DOS
- Microsoft Visual C++ 1.0
- Microsoft Visual C++ 1.5
- Microsoft Visual C++ 1.51
- Microsoft Visual C++ 1.52
- Microsoft Visual C++ 2.0
- Microsoft Visual C++ 2.1
- Microsoft Visual C++ 4.0
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
This article was previously published under Q47693 SUMMARY
When initializing a union, the initialization value is applied to the first
member of the union even if the type of the value matches a subsequent
member. As stated in the ANSI Standard, Section 3.5.7:
A brace-enclosed initializer for a union object initializes the
member that appears first in the declaration list of the union
type.
Because you cannot initialize the value of any member of a union other than
the first one, you must assign their values in a separate statement.
Initializing a union with a value intended for a subsequent member causes
that value to be converted to the type of the first member.
REFERENCES
For examples and explanation of possible compiler errors and warnings
generated when attempting to initialize a non-primary union element, please
see the following article in the Microsoft Knowledge Base:
39910 PRB: Initializing Non-Primary Union Element Produces Errors
Modification Type: | Minor | Last Reviewed: | 7/5/2005 |
---|
Keywords: | kbcode kbinfo kbLangC KB47693 |
---|
|