... please wait while loading ...

To view the complete site without permanently having to scroll, your browser-window should be maximized to support your screen-resolution of .
graphics
Handy Shop

facebook google

  17.376.975 Nedstat Basic - Kostenlose web site statistiken Persönliche Homepage webseite Zähler
Kostenlose Zähler
In memoriam C-BIT Information-Center Hannover (2.241/1075+2.241/1076 - no longer active)
Sicherheitshinweis: Wir weisen vorsorglich darauf hin, dass wir bei der Anmeldung saemtliche automatisch uebermittelten Parameter wie IP-Adresse und/oder Einwahlrufnummer speichern, um uns und unsere Kunden vor Missbrauch zu schuetzen. Selbstverstaendlich werden wir bei Bedarf umgehend strafrechtliche Massnahmen ergreifen, um einen vorliegenden Missbrauch zu ahnden.

INFO: Initializing Unions Initializes First Member of the Union

Article ID:47693
Last Review:July 5, 2005
Revision:2.1
This article was previously published under Q47693
On This Page

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.

Back to the top

MORE INFORMATION

The following example demonstrates the issue:

Back to the top

Sample Code

/* Compile options needed: none
*/ 

#include <stdio.h>
union { int   a;
        float b;
      } test = {3.6};    /* This is intended to initialize 'b'      */ 
                         /* however, the value will be converted    */ 
                         /* (first to a long and then to an int)    */ 
                         /* in order to initialize 'a'.             */ 

void main (void)
{
   float dummy = 0.0;            /* This causes the floating point  */ 
                                 /* math package to be initialized.  */ 
                                 /* Not necessary with VC++ for     */ 
                                 /* Windows NT.                      */ 

   printf ("test.a = %d,  test.b = %f\n", test.a, test.b);
}
				
The output from the example, though not what is intended, is as follows:
   test.a = 3, test.b = 0.00000
				
To associate a value with "b", you can reverse the order of the members, as in the following:
union {
        float b;
        int a;
      } test = {3.6};
				
Or, you can retain the order of the elements and assign the value in a separate statement, as in the following:
   test.b = 3.6;
				
Either of these methods creates the following output:
   test.a = 26214, test.b = 3.600000
				
Under Windows NT, the output would be as follows:
   test.a = 1080452710, test.b = 3.600000
				

Back to the top

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 (http://support.microsoft.com/kb/39910/EN-US/) PRB: Initializing Non-Primary Union Element Produces Errors

Back to the top


APPLIES TO
 Microsoft C Professional Development System 6.0a
 Microsoft Visual C++ 1.0 Professional Edition
 Microsoft Visual C++ 1.5 Professional Edition
 Microsoft Visual C++ 1.51
 Microsoft Visual C++ 1.52 Professional Edition
 Microsoft Visual C++ 2.0 Professional Edition
 Microsoft Visual C++ 2.1
 Microsoft Visual C++ 4.0 Standard Edition
 Microsoft Visual C++ 5.0 Enterprise Edition
 Microsoft Visual C++ 6.0 Enterprise Edition
 Microsoft Visual C++ 5.0 Professional Edition
 Microsoft Visual C++ 6.0 Professional Edition
 Microsoft Visual C++ 6.0 Standard Edition

Back to the top



This mirror is sponsored by:
Hansjoerg G. Henker
A-Z Consulting & Development
[438]

Information-Center [11.02.2012 22:35:45]