www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 40: Unions Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
We have another construct named union. The concept of union in C/C++ is: if we have something in the memory, is there only one way to access that memory location or there are other ways to access it. We have been using int and char interchangeably in our programs. Is it possible to have a memory location and use it as int or char interchangeably? For such purposes, the construct union is used. The syntax of union is:

union intOrChar{
int i,
char c;

The syntax is similar as that of structure. In structures, we have different data members and all of these have their own memory space. In union, the memory location is same while the first data member is one name for that memory location. However, the 2nd data member is another name for the same location and so on.

Consider the above union (i.e. intOrChar) that contains an integer and a character as data members. What will be the size of this union? The answer is very simple. The union will be allocated the memory equal to that of the largest size data member. If the int occupies four bytes on our system and char occupies one byte, the union intOrChar will occupy four bytes. Consider another example:

union intOrDouble{
int ival;

double dval;

The above union has two data members i.e. ival of type int and dval of type double. We know that double occupies more memory space than integer. Therefore, the union will occupy the memory space equivalent to double. The data members of unions are accessed in a similar way as we use with structures i.e. using the dot operator. For example:

intOrDouble uval;
uval.ival = 10;

To get the output of the data members, cout can be used as:

cout << “ The value in ival = “ << uval.ival;

It will print “The value in ival = 10”. Now what will be output of the following statement?

cout << “ The value in dval = “ << uval.dval;

We don’t know. The reason is that in the eight bytes of double, integer is written somewhere. When we use integer, it is printed fine. When we printed the double, the value of int will not be displayed. Rather something else will be printed. Similarly in the following statement i.e.

uval.dval = 100.0;
cout << “ The value in dval = “ << uval.dval;

It will print the right value of dval. The value of this double is written in such a way that it will not be interpreted by the integer. If we try to print out ival, it will not display 100. Unions are little bit safer for integer and characters. But we have to think in terms that where to store the value in memory. Unions are very rarely used.

Our C / C++ tutorial ends here. How did you find it? Please let us know at pickatutorial@yahoo.com | Tutorial author: farooq_fl@yahoo.com

More C++ / C Programming Tutorials

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.