Skip to main content

23. Structure

 

Why use Structure?

In C, there are many cases where we need to store multiple attributes of an entity. It is not necessary that an entity has all the information of one type only. It can have different attributes of different data types.

What is Structure

Structure in C is a user-defined data type that enables us to store the collection of different data types. Each element of a structure is called a member. It is used to create a complex data type which contains diverse information.
The struct keyword is used to define the structure.

Defining a structure for an entity employee in C :

struct employee {
    int id;
    char name[10];
    float salary;
};

The following image shows the memory allocation of the structure employee that is defined in above example.

Here, struct is the keyword; employee is the name of the structure; idname and salary are the members or fields of the structure.

Declaring Structure Variable

We can declare a variable for the structure so that we can access the member of the structure easily. There are two ways to declare structure variable :

  1. By struct keyword within main() function.
  2. By declaring a variable at the time of defining the structure.

1st Way:

The example to declare the structure variable by struct keyword is given below. It should be declared within the main function.

struct employee {
    int id;
    char name[50];
    float salary;
};

Now write the given code inside the main() function.

struct employee e1, e2;

The variables e1 and e2 can be used to access the values stored in the structure.

2nd Way:

Another example of declaring variable at the time of defining the structure.

struct employee {
    int id;
    char name[50];
    float salary;
}e1, e2;

Accessing members of the Structure

There are two ways to access structure members :

  1. By . (member or dot operator)
  2. By -> (structure pointer operator)
To access the member of the structure, we use this operator in between "Structure name" and "Member name".

#include <stdio.h>
#include <string.h>
struct employee{
    int id;
    char name[50];
}e1;
int main(){
    e1.id = 101;
    strcpy(e1.name, "Deepak Sharma");
    printf("Employee 1 id : %d\n", e1.id);
    printf("Employee 1 name : %s\n", e1.name);
    return 0;
}

Output :

Employee 1 id : 101
Employee 1 name : Deepak Sharma

#include <stdio.h>
#include <string.h>
struct employees{
    int ids;
    char naam[50];
    float salary;
}e2, e3;
int main(){
    e2.ids = 102;
    strcpy(e2.naam, "Chandni Sharma");
    e2.salary = 56000;
    e3.ids = 103;
    strcpy(e3.naam, "Himanshi Rathore");
    e3.salary = 126000;
    printf("Employee 2 id : %d\n", e2.ids);
    printf("Employee 2 name : %s\n", e2.naam);
    printf("Employee 2 salary : %10.2f\n", e2.salary);
    printf("Employee 3 id : %d\n", e3.ids);
    printf("Employee 3 name : %s\n", e3.naam);
    printf("Employee 3 salary : %10.2f\n", e3.salary);
    return 0;
}

Output :

Employee 2 id : 102
Employee 2 name : Chandni Sharma
Employee 2 salary : 56000.00
Employee 3 id : 103
Employee 3 name : Himanshi Rathore
Employee 3 salary : 126000.00

Nested Structure in C

C provides us the feature of nesting one structure within another structure by using which, complex data types are created.

The structure can be nested in the following ways :

  1. By Separate Structure
  2. By Embedded Structure

Separate Structure

Here, we create two structures, but the dependent structure should be used inside the main structure as a member.

#include <stdio.h>
#include <string.h>
struct Date{
    int dd;
    int mm;
    int yyyy;
};
struct Employee{
    int id;
    char name[20];
    struct Date doj;
}emp1;
int main()
{
    struct Employee emp1;
    printf("Enter employee id number: ");
    scanf("%d", &emp1.id);
    printf("Enter employee name: ");
    scanf("%s", &emp1.name);
    printf("Enter employee date of joining: ");
    scanf("%d %d %d", &emp1.doj.dd, &emp1.doj.mm, &emp1.doj.yyyy);
    printf("\nPrinting the employee Information....\n");
    printf("Employee id: %d\nEmployee name: %s\nEmployee date of joining: %d/%d/%d", emp1.id, emp1.name, emp1.doj.dd, emp1.doj.mm, emp1.doj.yyyy);
    return 0;
}

Output :

Enter employee id number: 101
Enter employee name: Deepak
Enter employee date of joining: 01 07 2002

Printing the employee Information....
Employee id: 101
Employee name: Deepak
Employee date of joining: 1/7/2002

Embedded Structure

The embedded structure enables us to declare the structure inside the structure. Hence, it requires less line to codes but it can not be used in multiple data structures.

#include <stdio.h>
#include <string.h>
struct employees{
    int ids;
    char naam[20];
    struct dates{
       int d;
       int m;
       int y;
    }dojs;
}emp2;
int main()
{
    struct employees emp2;
    printf("Enter employee id number: ");
    scanf("%d", &emp2.ids);
    printf("Enter employee name: ");
    scanf("%s", &emp2.naam);
    printf("Enter employee date of joining: ");
    scanf("%d %d %d", &emp2.dojs.d, &emp2.dojs.m, &emp2.dojs.y);
    printf("\nPrinting the employee Information....\n");
    printf("Employee id: %d\nEmployee name: %s\nEmployee date of joining: %d/%d/%d", emp2.ids, emp2.naam, emp2.dojs.d, emp2.dojs.m, emp2.dojs.y);
    return 0;
}

Output :

Enter employee id number: 102
Enter employee name: Himanshi
Enter employee date of joining: 21 06 2002

Printing the employee Information....
Employee id: 102
Employee name: Himanshi
Employee date of joining: 21/6/2002

Passing Structure to Function

We may pass the structure members into the function or pass the structure variable at once. Consider the following example to pass the structure variable employee to a function display() which is used to display the details of an employee.

#include <stdio.h>
struct address{
    char city[20];
    int pin;
    char phone[14];
};
struct employe{
    char names[20];
    struct address add;
}emp;
void display(struct employe);
int main()
{
    struct employe emp;
    printf("Enter employee name: ");
    scanf("%s", &emp.names);
    printf("Enter employee city: ");
    scanf("%s", &emp.add.city);
    printf("Enter employee city pin: ");
    scanf("%d", &emp.add.pin);
    printf("Enter employee phone number: ");
    scanf("%s", &emp.add.phone);
    display(emp);
    return 0;
}
void display(struct employe emp){
    printf("Printing Employee Details.....\n");
    printf("%s %s %d %s", emp.names, emp.add.city, emp.add.pin, emp.add.phone);
}

Output :

Enter employee name: Deepak
Enter employee city: Haldwani
Enter employee city pin: 263139
Enter employee phone number: 121212121212
Printing Employee Details.....
Deepak Haldwani 263139 121212121212

Why use an Array of Structures?

Consider a case, where we need to store the data of 20 employees. In that case, we will have to declare 20 different structure variables and store them one by one. This will always be tough since we will have to declare a variable every time we add an employee. Remembering the name of all the variables is also a very tricky task.By declaring an array of structures, we can make a collection containing all the structures that store the information of different entities.

Array of Structures in C

An array of structures in C can be defined as the collection of multiple structure variables where each variable contains information about different entities. The array of structures in C are used to store information about multiple entities of different data types. The array of structures is also known as the collection of structures.

Note: Use fflush(stdin); after scanf(), if you want to use the gets() function.

#include <stdio.h>
#include <string.h>
struct student{
    int rollno;
    char name[10];
};
int main(){
int i;
struct student st[5];
printf("Enter Records of 5 Students\n");
for (i = 0; i>5; i++){
    printf("Enter Roll Number: ");
    scanf("%d", &st[i].rollno);
    printf("Enter Name: ");
    scanf("%s", &st[i].name);
}
    printf("\nStudent Information List :");
    for (i = 0; i>5; i++){
    printf("\nRoll Number: %d, Name: %s", st[i].rollno, st[i].name);
    }
}

Output :

Enter Records of 5 Students
Enter Roll Number: 1
Enter Name: Chandni
Enter Roll Number: 2
Enter Name: Deepak
Enter Roll Number: 3
Enter Name: Himanshi
Enter Roll Number: 4
Enter Name: Kamal
Enter Roll Number: 5
Enter Name: Umesh

Student Information List :
Roll Number: 1, Name: Chandni
Roll Number: 2, Name: Deepak
Roll Number: 3, Name: Himanshi
Roll Number: 4, Name: Kamal
Roll Number: 5, Name: Umesh

Comments