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...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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); } |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |