IT Community - Software Programming, Web Development and Technical Support

what is the UNION in c++? with Example

This is a discussion on what is the UNION in c++? with Example within the C and C++ Programming forums, part of the Software Development category; Hi, Any one Explain the Question Thanks Sundar Raja...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-20-2007, 07:15 AM
sundarraja sundarraja is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 174
sundarraja is on a distinguished road
Default what is the UNION in c++? with Example

Hi,

Any one Explain the Question

Thanks
Sundar Raja
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-20-2007, 07:44 AM
Senthilkumar Senthilkumar is offline
D-Web Programmer
 
Join Date: Mar 2007
Posts: 93
Senthilkumar is on a distinguished road
Default Re: what is the UNION in c++? with Example

A union is a user-defined data or class type that, at any given time, contains only one object from its list of members (although that object can be an array or a class type).

For example, you need to store color data as four 8-bit unsigned char numbers. At the same time, you have to store this color data as one 32-bit unsigned integer. Union allows to use both representation(struct with 4 unsigned char and unsigned integer) at the same time, using same block of computer's memory.

The union is declared as:

union union-type-name { union-list } union-variable;
In this form, union-type-name is optional. However, if you want to use union type in several places, it is better to use another way of enum declaration:
union union-type-name { union-list };

//... (and somewhere below)

union union-type-name union-variable;
Of course, in the second case union-type-name cannon be omitted.

Example: declaring union type for color data

source code: C++

union u_color {
// first representation (member of union)
struct s_color {
unsigned char a, b, g, r;
} uc_color;

// second representation (member of union)
unsigned int i_color;
};
?>
After declaration, it is possible to use union-type-name as user-defined type.

Following example demonstrates basic union usage. It reads file which contains 32 bit unsigned ints and decodes it to four 8 bit RGBA (Red, Green, Blue, Alpha-Transparency) values.

source code: C++

#include <iostream>
#include <stdio.h>
using namespace std;

union u_color {
// first representation (member of union)
struct s_color {
unsigned char a, b, g, r;
} uc_color;

// second representation (member of union)
unsigned int i_color;
};

int main(void) {
FILE *f = fopen("color.dat", "rb");
if (f == NULL)
exit(1);

fseek(f, 0, SEEK_END);
size_t size = ftell(f);
// file size in bytes
fseek(f, 0, SEEK_SET);
size /= 4;
// file size in 32bit ints

for(size_t i=0; i<size; i++)
{
u_color clr;
//reading from tile to clr.i_color
fread(&clr.i_color, sizeof(unsigned int), 1, f);

// printing from clr.uc_color to output stream
cout << "R=" << int(clr.uc_color.r) << " ";
cout << "G=" << int(clr.uc_color.g) << " ";
cout << "B=" << int(clr.uc_color.b) << " ";
cout << "A=" << int(clr.uc_color.a) << endl;
}

fclose(f);
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Make out of Structure and Union velhari C and C++ Programming 2 04-03-2008 03:55 AM
Union and Union All itbarota Database Support 2 03-03-2008 01:41 AM
UNION ALL faster than a UNION? vadivelanvaidyanathan Database Support 4 08-18-2007 02:04 AM
What is the difference between UNION and UNIONALL in SQL Server? bluesky Database Support 1 07-25-2007 05:53 AM
What's the difference between a structure and a union prasath C and C++ Programming 1 07-18-2007 05:14 AM


All times are GMT -7. The time now is 09:39 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0