Whats New for Visual C Version 6?

Chapter 13 - Structures, Unions, and Miscellaneous Items

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Unions
A union is another data type that can be used in many distinctive ways. A specific union, for example, could be construed as an integer in one operation and a float or double in another operation. Unions have an appearance similar to structures. However, they are very dissimilar. Like a structure, a union can contain a group of many data types. In a union, however, those data types all share the same location in memory! Thus, a union can contain information on only one data type at a time. Many other high-level languages refer to this capability as a “variant record.”
Unions: Syntax and Rules
A union is constructed by using the keyword union and the syntax that follows:
union tag_field {
 
type field1;
 
type field2;
 
type field3;
      .
      .
      .
 
type fieldn;
};
A semicolon is used for termination because the structure definition is actually a C and C++ statement.
Notice the declaration syntax similarities between structures and unions in the following example declaration:
union unmany_types {
 char c;
 int ivalue;
 float fvalue;
 double dvalue;
} unmy_union
The union is defined with the keyword union followed by the optional tag field, unmany_types. The union’s optional tag field operates exactly the way its structure counterpart does. This union contains several members: a character, an integer, a float, and a double. The union will allow unmany_types to save information on any one data type at a time.
The variable associated with the union is unmy_union. If this statement is contained in a function, the union is local in scope to that function. If the statement is contained outside of all functions, the union will be global in scope.
As with structures, it is also possible to associate several variables with the same union. Also like a structure, members of a union are referenced by using the dot (.) operator. The syntax is simply
unname.mname
In this case, unname is the variable associated with the union type and mname is the name of any member of the union.
Constructing a Simple Union
In order to illustrate some concepts about unions, the following C++ program creates a union of the type just discussed. The purpose of this example is to show that a union can contain the definitions for many data types but can hold the value for only one type at a time.
//
//  unions.cpp
//  C++ program demonstrates the use of a union.
//  A union is created with several data types.
//  Copyright (c) Chris H. Pappas and William H. Murray, 1997
//

#include <iostream.h>

union unmany_types {
 char c;
 int ivalue;
 double fvalue;
 double dvalue;
} unmy_union;
int main(void)
{
 // valid I/O

 unmy_union.c=’b’;
 cout << unmy_union.c << “\n”;

 unmy_union.ivalue=1990;
 cout << unmy_union.ivalue << “\n”;

 unmy_union.fvalue=19.90;
 cout << unmy_union.fvalue << “\n”;

 unmy_union.dvalue=987654.32E+13;
 cout << unmy_union.dvalue << “\n”;

 // invalid I/O
 cout << unmy_union.c << “\n”;
 cout << unmy_union.ivalue << “\n”;
 cout << unmy_union.fvalue << “\n”;
 cout << unmy_union.dvalue << “\n”;

 // union size
 cout << “The size of this union is: ”
      << sizeof(unmany_types) << “ bytes.” << “\n”;

 return (0);
}
The first part of this program simply loads and unloads information from the union. The program works because the union is called upon to store only one data type at a time. In the second part of the program, however, an attempt is made to output each data type from the union. The only valid value is the double, since it was the last value loaded in the previous portion of code.
b
1990
19.9
9.876543e+18

-154494568
-2.05461e+033
9.87654e+018
The size of this union is: 8 bytes.
Unions set aside storage room for the largest data type contained in the union. All other data types in the union share part, or all, of this memory location.
By using the integrated debugger, you can get an idea of what is happening with storage within a union.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net