Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Structure And Union in C and C++

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:

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.

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.

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:

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:

The output of the code shown below:

Output of the C++ union example.
Output of the C++ union example.

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?

Allocates different memory location to the individual members of itAll 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 timeOne can use only one member at a time.

Categories: C++

Tags: , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.