Structures and Unions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Structures and Unions

Structures:
Means of creating a user-defined datatype that combines multiple types of data into a single datatype.
Syntax:
struct struct_name
{
//Data_members
};
struct_name acts as a user-defined datatype. Data members are defined just like a normal variable
definition without initialization.

Example:
struct human
{
char name[10];
int age;
char gender;
};
In the above example human acts as the user-defined datatype, with which we can create
objects/variables which in turn each variable/object can store string, integer and character data at the
same time.

Consider the following program:


#include<stdio.h>
struct human //created a datatype called human
{
char name[10];
int age;
char gender;
};

int main()
{
struct human h1 = {“Cprogram”, 100, ‘M’}; //Created and initialized an object called h1 of human
printf(“%s”, h1.name); //using . operator to access data members using a normal object
printf(“\n%d”,h1.age);
printf(“\n%c”,h1.gender);
return 0;
}

To access the data members of a structure we have to use member access operators. There are 2
member access operators:
1) . (dot) : used with normal objects
2) -> (arrow) :used with pointer objects
Every data member of the structure is associated with an independent memory location ie structures
create multiple memory locations for every data member.
For the above example, the structure human creates 3 memory locations for the data members name,
age and gender.
Unions:
Unions are similar to structures, means of creating a user-defined datatype that combines multiple
types of data into a single datatype. The primary difference between structures and Unions is that,
structures create multiple memory locations for its data members whereas Unions create just a single
memory location for all of its data members with the size of the memory location being the largest
size of data member.

Syntax for Union definition:

union union_name
{
//Data_members
};

union_name acts as a user-defined datatype. Data members are defined just like a normal variable
definition without initialization.

Example:
union human
{
char name[10];
int age;
char gender;
};

In the above example human acts as the user-defined datatype, with which we can create
objects/variables which in turn each variable/object can store string, integer and character data at the
same time.

Consider the following program:

#include<stdio.h>
union human //created a datatype called human
{
char name[10];
int age;
char gender;
};

int main()
{
union human h1; //Created an object called h1 of human

strcpy(hi.name,”CProgram”);
printf(“%s”, h1.name); //using . operator to access data members using a normal object
h1.age=100;
printf(“\n%d”,h1.age);
hi.gender=’F’;
printf(“\n%c”,h1.gender);
return 0;
}
To access the data members of a union, we have to use member access operators. There are 2
member access operators:
1) . (dot) : used with normal objects
2) -> (arrow) :used with pointer objects
All data members of the union are associated with a single memory location ie unions create a single
memory location for all data members. Only one memory location with the size of the largest memory
location ie the size of the name data member will be created to hold the data related to all the data
members.

You might also like