union vs structure

This page on union vs structure describes difference between union and structure. Both structure and union store more than one variables of the different data type. Difference lies in the way these variables are allocated memory in both the cases. Let us understand the concept with following example c program.


struct emp_record
{ char name[10];
int number[12];
float Salary;
};

union emp_record
{
char name[10];
int number[12];
float Salary;
};

For structure, total memory allocated is about 7 bytes, which include 1 byte for char,2 for int and 4 for float. In structure all the member variables are stored consecutively one after the other. For union, total memory allocated is about 4 bytes, which will be shared by all the variables at different time instants. In union total size is allocated equal to size of the maximum size of the member variable.

RELATED LINKS

C Programming Language tutorial

What is Difference between

difference between FDM and OFDM
Difference between SC-FDMA and OFDM
Difference between SISO and MIMO
Difference between TDD and FDD
Difference between 802.11 standards viz.11-a,11-b,11-g and 11-n
OFDM vs OFDMA
CDMA vs GSM
Bluetooth vs zigbee
Fixed wimax vs mobile

RF and Wireless Terminologies