Structures in C Programming

Structures in C: In C Programming, Arrays are helpful for storing a group of similar data type elements. But, there are some situations where we have to group non-similar data types (int, float, char, etc.). This C programming language introduced the concept of Structures to handle these types of situations.

The struct keyword is used to create structures in C programming. These are used to group different data types to organize the data in a structural way.

For example, we are storing employee details such as name, id, age, address, and salary. From the names, you can understand that they are not the same data type. Normally we create a separate variable for the name, id, address, age, and salary, but how about storing the same for 5 employees? It will be difficult for the developer to assign the variable names to each. So, We create a C structure using struct Keyword and assign the name as an employee.

C Structures Syntax

The basic syntax of the structures using struct is as shown below

struct Structure_Name
{
  Data_Type Variable_Name;

  Data_Type Variable_Name;
 
  ………….
};

The items available in the syntax of struct or structures

  • The struct is the system reserved keyword used to create and access structures.
  • Structure_Name: Name you to desire to give. For example, employees, person, students.
  • Data_Type: Data type of the declared variable. For example, int, float, and char.
  • Variable_Name – For example, id, name, age, salary.

struct Example

struct Employee // Name = Employee

{
  int age; //Data_Type = int; Variable Name = age

  char name[50]; //Data Type = char Array; Variable_Name = name

  float salary; //Data Type = float; Variable Name = salary
};

Declaration of Structures in C Programming

Members inside the C structures will not store any memory location until they are associated with variables. So, we have to create the struct variable before using it. We can declare the struct variables in multiple ways.

First Approach

Declaring the C structure first and then in the main function creating the struct variable.

struct Employee
{
  int age;

  char name[50];

  float salary;
};

//Creating the Struct variable in main() function
struct Employee emp1, emp2;

emp1 = {25, “Dave”, 25000};

emp1and emp2 = variables of struct data type.

Second Approach

We are creating the struct variables at the time of the structure declaration.

struct Employee
{
  int age;

  char name[50];

  float salary;

} emp1, emp2;

Accessing Members of Structures in C

We can locate the structure members using the dot (.) operator or members operator. Let us use the above struct example.

//Assigning the values

 emp2.age = 26;

 strcpy (emp2.name, “Michel”);

 emp2.salary = 46000.50;

Structures in C Programming Example

In this example program, we declare the structure with 5 data members. Next, we display each item as output.

#include <stdio.h> 
#include <string.h> 

struct Employee 
{
  int Empolyee_ID;
  int age;  
  char Name[50];
  char Department[20];
  float Salary;
};

int main() 
{
  struct Employee emp1 = { 101, 25, "Dave", "IT", 25000.50 };
  struct Employee emp2;
  
     emp2.Empolyee_ID = 102;
     emp2.age = 28;
     strcpy(emp2.Name, "Christofer");
     strcpy(emp2.Department, "Science");
     emp2.Salary = 32000.70;

  printf(" Details of the Employee1 \n " );
  printf(" Employee Id = %d \n ", emp1.Empolyee_ID );
  printf(" Employee Age = %d \n ", emp1.age );
  printf(" Employee Name = %s \n ", emp1.Name );
  printf(" Employee Department = %s \n ", emp1.Department );
  printf(" Employee Salary = %.2f \n\n ", emp1.Salary );

  printf(" Details of the Employee1 \n " );
  printf(" Employee Id = %d \n ", emp2.Empolyee_ID );
  printf(" Employee Age = %d \n ", emp2.age );
  printf(" Employee Name = %s \n ", emp2.Name );
  printf(" Employee Department = %s \n ", emp2.Department );
  printf(" Employee Salary = %.2f \n ", emp2.Salary );

  return 0;
}
Structures in C Programming

In this C structures example, we declared an Employee with Empolyee_ID, age, Name, Department, and Salary members with appropriate Data Types. Please refer to the Arrays article in C Programming.

Within the main() function, We created the Employee struct variable emp1 and assigned the values to them.

struct Employee emp1 = {101, 25, "Dave", "I T", 25000.50};

It means the values for the emp1 struct are as follows

Empolyee_ID = 103

age = 25

Name = Dave

Department = I T

Salary = 25000.50

We created one more Employee C structure variable, emp2. This time, we used the member’s or dot operator (.) to specify the values based on the data type. The printf statements are to output each member present in the struct variables.

NOTE: Using the member’s operator, we can assign values to the int and float variables. However, for strings, we have to use the string strcpy function only.

Nested Structures in C Programming

Nested Structures in C: In our previous post, we discussed Structures, which are pretty good for grouping different data type members. However, there are some problems, such as listing more number of variables inside the structure and Code repetitiveness. To resolve these situations, we use C Nested structures.

The Nested Structures in C example: Assume we are working with Employee, Student, and Person data. So, we want to declare three, i.e., Employee, Student, and Person. Each one may have different members (Ex: For Students: Roll Number, Class Number, Marks, etc.). But there may be some common members such as Age, door number, street name, and city. Adding door numbers, street names, and city members to all three structures generally means repetitive work!

Like Loops, Nested structures in C programming are the Structures inside the other. Using this, we can create the first one called to address and add the door number, street name, and city members under it. We can call this address from the Employee, Student, and Person. We can declare the nested structure in multiple ways:

Nested Structures in C Programming First Approach

First, declare the address structure and create the address variable inside the Employee. Please refer to the Introduction and Loops articles.

Declaring the address Struct

struct address
{
  int Door_Number;

  char Street_Name[20];

  char City[20];

  int Postcode;

} ;

Here, declaring the Employee Struct

struct Employee
{
  int age;

  char Name[50];

  char Department[20];

  struct address add; // creating the address variable = add

  float salary;

} ;

Second Approach

In this Nested Structures in C Programming example, I declare the address with its members directly inside the Employee struct.

struct Employee
{
  int age;
  char Name[50];
  char Department[20];
  float salary;

  struct address // Declaring the address
   {
     int Door_Number;
     char Street_Name[20];
     char City[20];
     int Postcode;
   } add; // creating the address variable = add
};

Program to Display Nested Structures in C Programming

In this C Program, we will declare Nested structures (one inside the other). Then we are going to display them in two ways:

  1. Create a Structure Variable and assign the corresponding values to them (Normal Variable)
  2. Create Struct Pointer Variable and assign the corresponding values to them (Pointer Variable)

In this C Nested Structures example, we declared the address struct with Door_Number (integer data type), and City (character array of 20 bytes or, say, string)

Next, declare the Employee with Age, Name, Salary, and struc variable of the address and add members with appropriate Data Types. From the below C Programming code, you can observe that we created the pointer variable (i.e., *emp3) at the time of struct declaration only.

#include<stdio.h>
#include<string.h>

struct address
{
  int Door_Number;
  char City[20];
};

struct Employee
{
  int Age;
  char Name[50];
  struct address add;
  float Salary;  
}*emp3;

int main() 
{
  struct Employee emp1 = { 25, "Tutorial", 222, "Liverpool", 25000.50 };
  struct Employee emp2;
    
  emp2.Age = 28;
  strcpy(emp2.Name, "Gateway" );
  
  emp2.add.Door_Number = 145;
  strcpy(emp2.add.City, "Manchester" );
  
  emp2.Salary = 45000.00; 
  
  emp3 = &emp2;
  
  printf("\n Details of the Employee 1 \n " );
  printf(" Employee Age = %d \n ", emp1.Age );
  printf(" Employee Name = %s \n ", emp1.Name );
  printf(" Employee Door Number = %d \n ", emp1.add.Door_Number );
  printf(" Employee City = %s \n ", emp1.add.City );
  printf(" Employee Salary = %.2f \n\n ", emp1.Salary );

  printf(" Details of the Employee 2 \n " );
  printf(" Employee Age = %d \n ", emp3->Age );
  printf(" Employee Name = %s \n ", emp3->Name );
  printf(" Employee Door Number = %d \n ", emp3->add.Door_Number );
  printf(" Employee City = %s \n ", emp3->add.City );
  printf(" Employee Salary = %.2f \n ", emp3->Salary );
  
  return 0;
}
Nested Structures in C Programming

Within the main() function, we created the Employee struct variable emp1 and then assigned the values to them.

struct Employee emp1 = { 25, "Tutorial", 222, "Liverpool",25000.50 };

It means the values for emp1 are as follows.

Age = 25

Name = Tutorial

Door Number = 222 //address variable

City = Liverpool //address variable

Salary = 25000.50

Created one more variable, emp2. This time, we used the member’s operator or dot operator (.) to assign the values as per the data type.

struct Employee emp2;

emp2.Age = 28;

We have to use the strcpy() built-in function to assign values for the strings.

strcpy(emp2.Name, "Gateway");

emp2.Salary = 45000.00;

The door Number and City are the members of the address structure. So, to access them, we have to use the below format in C Nested Structures:

Variable. Nst_Variable. Member

Here, the Variable is emp2, and the Nst_Variable added so,

emp2.add.Door_Number = 145;

strcpy(emp2.add.City, "Manchester");

Assigning the address of the emp2 to variable emp3 using the below statement

emp3 = &emp2;

The Printf statement is to output each member present in the structure variables.

C Nested Structures Points to Remember

  1. To print the structure variable, we must use the dot operator (.)
  2. To print the nested structure variable, we have to use two dot operators such as (Variable. Nst_Variable. Member)
  3. printing the pointer variable, we have to use an arrow operator instead of the dot operator. (Variable -> Member)
  4. To print the nested variable using pointers, we have to use a combination of the arrow operator and the dot operator. (Variable -> Nst_Variable.Member)