Structures and Unions in C Programming

Structures

In the previous chapter, Arrays we have found that arrays are being used to store data of the same type. But often we come across the collection of that of different types that can be considered a unit. For example, in the register book that contains the employee details working in a company, the table storing this data would contain employee’s name, id, age, nationality, etc. For storing this kind of data, we use the concept known as structures.

A structure can be defined as a collection of related data elements stored as a single unit. The individual structure elements are referred to as members.

To define a structure the keyword struct is being used. It informs the compiler that a structure is being declared. It is then followed by the structure name. Its members are declared within the curly braces that come after the structure name.

It can have the syntax of the form as shown below:

[php]

struct Structure_Name{

member1;

member2;

.

.

.

memberN;

};

[/php]

For example, let us create a structure for the mentioned above and name it as Employee.

[php]

struct Employee{

int id;

char name[50];

int age;

char nationality[30];

} ;

[/php]

Once the structure is being declared create the structure variables. It can be then populated as shown below, in the order as defined above:

struct Employee employee1 = {1, “Sonam”, 30, “Bhutanese”};

To access these values, for example, to get the employee name it can be accessed by using employee1.name as shown in the example given below.

Example 16: Illustrate the use of structures in C.

Sample Code:

[php]

#include <stdio.h>

//structure.c by bhutanio.com

//structure definition

struct student

{

int id;

char name[50];

int age;

};

void main(){

//creating variables and initializing members

struct student student1 = {1, "Sonam", 25};

struct student student2 = {2, "Dema", 18};

struct student student3 = {3, "Dorji", 15};

//accessing members structure

printf("%d", student1.id);

printf("\n%s", student1.name);

printf("\n%d", student1.age);

}

[/php]

In the above example, structure variable declaration is done one by one. There is a way that these variables can be created during the structure definition too. For instance,

struct student

{

int id;

char name[50];

int age;

}student1, student2, student3;//write the variables here

Structures can be nested that is structure(s) can be added to another structure as a member. For example, in the above examples for the structure student, there are no address details. Say we need to add the address to this data. To do this, define another structure, say the address.

struct address{

char streetname[50];

char townname[50];

}

Now to add address to student create the variable of address in student.

[php]

struct student

{

int id;

char name[50];

int age;

struct address addr;

}

[/php]


Unions

Unions are like structures except that it can store only one field. It has the similar definition of the structures. It can be defined with the keyword union as shown below.

[php]

union union_name{

Type member1;

Type member2;

.

.

.

Type memberN;

};

[/php]

For example,

[php]

union data{

int x;

char y;

}

[/php]

For a union, the compiler allocates the memory such that the size is equal to the member that requires the largest memory. Here, we now that integer would take a memory of 4 bytes and 1 byte by character variable. Thus, the memory allocated to the union data is 4 bytes. The variable y would share the memory from the variable x.

Then declare a union variable. For the above example:

union data var;

To initialize the union to a union variable, we use dot operator like in structures. We can also use pointers to do this. To do it with pointer we use arrow operator.

Let us look into an example that uses dot operator.

Example 17: Illustrate the use of unions.

[php]

#include <stdio.h>

//unions.c by bhutanio.com

int main(){

union data{

int x;

char y;

};

union data var;

var.x = 2;

printf("\n%d ", var.x);

}

[/php]

Leave a Reply

Discover more from BHUTAN IO

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top