1. Introduction
The programming languages C and C++ both supports Structure and Union. Structure and union are user-defined data types and they differ based on the memory allocation. Moreover, they also behave differently in C and C++.
2. Declaring a Structure
A Structure is a user-defined data type, which is nothing but a combination of standard data types. The below code snippet shows how it will be declared:
1 2 3 4 5 6 |
struct dept { int deptid; char deptname[20] ; float avgEmpSal; }; |
Note, a Structure can have another Structure as member. However, still we can peel it off and end up with standard data types.
3. Structure in C++
The structure name is specified after the struct keyword. Inside the structure, you can place the standard data types and they together form a unit called, User-Defined Data Type. Usually, we call these standard types which is housed in the structure as Data Members.
The below statement creates a user-defined variable or structure of type dept.
struct dept sales;
To access the members of a Structure, we can use the Dot Operator. The below code snippet shows how we can do it:
sales.deptid = 101;
4. Structure in C++: An Example
Below example shows how we can declare and use the structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
struct dept { int deptid; char deptname[20] ; float avgEmpSal; }; int _tmain(int argc, _TCHAR* argv[]) { struct dept sales; //struct dept purchase; sales.deptid = 101; sprintf(sales.deptname, "%s", "Sales Dept"); sales.avgEmpSal = 150000.45f; printf("Printing the Structure Data"); printf("%s has spent %f$ for salary", sales.deptname, sales.avgEmpSal ); getch(); } |
In this example, first, a Structure called dept
is declared with three internal data members of standard types int, char, and float. Note that the declaration ends with a semicolon.
In the main program, the statement struct dept sales creates the Structure. Here, the term dept denotes the user-defined type name or a template and ‘sales’ is a real object that will stay in memory. In the remaining part of the code, you can see how one can use Structure members using the Period Operator.
5. How Structure Differ Between C and C++
The programming languages C and C++ differs in a way they treat Structure and Union. In C language, a Structure can contain only the data members. But in C++ it can have one or more functions along with their data members.
In C++, we no need to specify the keyword struct while declaring a Structure. But we do not need it to have the typedef keyword in the declaration. In C, we have to specify the ‘struct’ keyword before declaring the Structure variable. Or, the Structure declaration should have the typedef.
1 2 3 4 5 6 7 8 9 10 11 |
struct st { int memebr1; int memebr2; }; int _tmain(int argc, _TCHAR* argv[]) { st var1; var1.memebr1 = 15; } |
The above code snippet is for C++. Note, there is no struct as well as typedef keyword when we are declaring the variable var1 .
6. Unions in C++
Union is a basic C data type. Like the Structure, it also can have data members. Then how it differs from the Structure?
If a Structure has 10 data members, space is allocated for all 10 data members. But, with a Union, the compiler finds the largest data member and allocates space for it. We can get access to the Union members in the same way how we access the Structure members. In Union, we can use only one member at a time as all members share same memory location.
7. Declaring an Union
Below is the example for declaring a simple Union data type:
1 2 3 4 5 6 |
union Department { int deptid; long numberofEmp; float total_salary_expense; }; |
In the above declaration, the Union is named as Department using the keyword union. It has three data members. So, the size allocated for the Union is a size of the float. It is because the float requires more bits for its storage when compared with other members.
If a Union allocates storage for the largest member, Is it possible to assign and retrieve a value for all the member at the same time? No is the answer. Because the values are overwritten as the allocated space is shared by all the members.
Each Structure member will have their own memory slot. But, all Union members share a single memory slot.
8. Union in C++: An Example
The Example program code is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
union Department { int deptid; long numberofEmp; float total_salary_expense; }; int _tmain(int argc, _TCHAR* argv[]) { union Department dept; dept.deptid = 100; printf("Dept id = %d \n", dept.deptid ); dept.numberofEmp = 15000; printf("Total Employee = %ld \n", dept.numberofEmp ); printf("Try to Print Dept Id = %d", dept.deptid ); getch(); return 0; } |
The output of the code shown below:

Even though we accessed the DeptId, we got 15000 as the value in the last line of the output. Since there is only a single storage (The storage required for the largest among the member) and it is shared by all three members, we got 15000 for dept id in the last line.
9. What is the Difference Between Structure and Union?
Structure | Union |
Allocates different memory location to the individual members of it | All the members share a single allocation given to it |
Since, all the members have their own allocation, the size depends on all the members of it. | The memory is allocated to largest member of the union and all other members shares this location. Hence the size of the union depends on the largest member present in it |
One can use all the members at the same time | One can use only one member at a time. |
Categories: C++