Structure And Union
Structure
Structures (also called structs) are a way to group several related variables into one
place. Each variable in the structure is known as a member of the structure.
Unlike an array, a structure can contain many different data types (int, float, char,
etc.).
A structure is a key word that create user defined data type in C/C++. A structure
creates a data type that can be used to group items of possibly different types into
a single type.
Create a Structure
You can create a structure by using the struct keyword and declare each of its
members inside curly braces:
struct MyStructure { // Structure declaration
int myNum; // Member (int variable)
char myLetter; // Member (char variable)
}; // End the structure with a semicolon
Syntax:
struct
{
// Declaration of the struct
}
1
Union
A union is a type of structure that can be used where the amount of memory used
is a key factor. Similarly to the structure
Union are used for combining informatintion,where only single data is required at
a time.
Create a Union
Union is a user-defined datatype. All the members of union share same memory
location. Size of union is decided by the size of largest member of union. If you
want to use same memory location for two or more members, union is the best for
that.
Unions are similar to structures. Union variables are created in same manner as
structure variables. The keyword “union” is used to define unions in C language.
Here is the syntax of unions in C language,
union union_name {
member definition;
} union_variables;
A union is a type of structure that can be used where the amount of memory used is
a key factor.
Similarly to the structure, the union can contain different types of data types.
Each time a new variable is initialized from the union it overwrites the
previous in C language but in C++ we also don’t need this keyword and uses
that memory location.
This is most useful when the type of data being passed through functions is
unknown, using a union which contains all possible data types can remedy
this problem.
It is declared by using the keyword “union“.
2
Example :
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
} data, data1;
int main( ) {
printf( "Memory size occupied by data : %d\t%d", sizeof(data), sizeof(data1));
return 0;
}
Out put:
Memory size occupied by data : 4 4
3
Structure program:
#include <stdio.h>
#include <string.h>
// create struct with person1 variable
struct Person {
char name[50];
int citNo;
float salary;
} person1;
int main() {
// assign value to name of person1
strcpy(person1.name, "George Orwell");
// assign values to other person1 variables
person1.citNo = 1984;
person1. salary = 2500;
// print struct variables
printf("Name: %s\n", person1.name);
printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f", person1.salary);
return 0;
}
4
Output
Name: George Orwell
Citizenship No.: 1984
Salary: 2500.00
5
Union program:
// C++ program to illustrate the use
// of the unions
#include <iostream>
using namespace std;
// Defining a Union
union GFG {
int Geek1;
char Geek2;
float Geek3;
};
// Driver Code
int main()
{
// Initializing Union
union GFG G1;
G1.Geek1 = 34;
// Printing values
cout << "The first value at "
<< "the allocated memory : " << G1.Geek1 << endl;
G1.Geek2 = 34;
cout << "The next value stored "
<< "after removing the "
<< "previous value : " << G1.Geek2 << endl;
G1.Geek3 = 34.34;
cout << "The Final value value "
<< "at the same allocated "
<< "memory space : " << G1.Geek3 << endl;
return 0;
}
6
Output:
The first value at the allocated memory : 34
The next value stored after removing the previous value : "
The Final value value at the same allocated memory space : 34.34
7
8