Detailed Explanation of Structures in C Language

Why Use Structures?

In C language, there are situations where multiple attributes of an entity need to be stored. Not every entity has all information of only one type. It can have different attributes of different data types. For example, an entity “student” may have a name (string), roll number (integer), and marks (float). To store such information related to the student entity, we have the following methods:

  • Construct separate arrays to store names, roll numbers, and marks.

  • Use special data structures to store collections of different data types.

Let’s explore the first method in detail.

#include <stdio.h>void main(){  char names[2][10], dummy; // Use a two-dimensional character array names to store students' names  int roll_numbers[2], i;  float marks[2];  for (i = 0; i < 3; i++)  {      printf("Enter the name, roll number, and marks of student %d", i + 1);      scanf("%s %d %f", &names[i], &roll_numbers[i], &marks[i]);      scanf("%c", &dummy); // Store newline character in dummy character in each iteration  }  printf("Printing the student details...\n");  for (i = 0; i < 3; i++)  {      printf("%s %d %f\n", names[i], roll_numbers[i], marks[i]);  }}

Output

Enter the name, roll number, and marks of student 1: Arun 90 91        Enter the name, roll number, and marks of student 2: Varun 91 56      Enter the name, roll number, and marks of student 3: Sham 89 69
Printing the student details...Arun 90 91.000000                                                                      Varun 91 56.000000  Sham 89 69.000000

The program above meets our requirement to store information about the entity “student”. However, the program is quite complex, and the complexity increases with the number of inputs. Each element of the arrays is stored continuously, but not all arrays need to be stored in memory continuously. The C language provides another simpler method, using a special data structure called a structure, where we can group information of different data types about an entity.

đŸ‘‡ Click to Claim đŸ‘‡
đŸ‘‰ C Language Knowledge Resource Collection

What is a Structure

In C language, a structure is a user-defined data type that allows us to store a collection of different data types. Each element of a structure is called a member. Structures can mimic the use of classes and templates as they can store various information.

Use the struct keyword to define a structure. Let’s look at the syntax for defining a structure in C language.

struct structure_name{  data_type member1;  data_type member2;  .  .  data_type memberN;};

Let’s see an example of defining a structure for the “employee” entity in C language.

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

The illustration below shows the memory allocation of the structure “employee” defined in the above example.

Detailed Explanation of Structures in C Language

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

Declaring Structure Variables

We can declare a structure variable to easily access the members of the structure. There are two ways to declare structure variables:

  1. Declare using the struct keyword inside the main() function

  2. Declare variables at the time of defining the structure.

First Method:

Let’s see an example of declaring a structure variable using the struct keyword. It should be declared inside 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. Here, e1 and e2 can be treated like objects in C++ and Java.

Second Method:

Let’s look at another way to declare variables at the time of defining the structure.

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

Which Method is Better

If the number of variables is not fixed, use the first method. It gives you the flexibility to declare structure variables multiple times in the main() function.

If the number of variables is fixed, use the second method. This saves code for declaring variables in the main() function.

Accessing Structure Members

There are two ways to access structure members:

  1. Using . (member operator or dot operator)

  2. Using -> (structure pointer operator)

Let’s see the code to access the id member of the p1 variable using the . (member) operator.

p1.id

Example of C Structure

Let’s look at a simple example of a structure in C language.

#include<stdio.h>#include<string.h>
struct employee{  int id;  char name[50];} e1; // Declare variable e1 for the structure
int main(){  // Store the information of the first employee  e1.id = 101;  strcpy(e1.name, "Sonoo Jaiswal"); // Copy string to character array
  // Print the information of the first employee  printf("Employee 1 ID: %d\n", e1.id);  printf("Employee 1 Name: %s\n", e1.name);
  return 0;}

Output:

Employee 1 ID: 101Employee 1 Name: Sonoo Jaiswal

Let’s look at another example of a structure in C language that stores information of multiple employees.

#include<stdio.h>#include<string.h>
struct employee{  int id;  char name[50];  float salary;} e1, e2; // Declare variables e1 and e2 for the structure
int main(){  // Store the information of the first employee  e1.id = 101;  strcpy(e1.name, "Sonoo Jaiswal");  e1.salary = 56000;
  // Store the information of the second employee  e2.id = 102;  strcpy(e2.name, "James Bond");  e2.salary = 126000;
  // Print the information of the first employee  printf("Employee 1 ID: %d\n", e1.id);  printf("Employee 1 Name: %s\n", e1.name);  printf("Employee 1 Salary: %f\n", e1.salary);
  // Print the information of the second employee  printf("Employee 2 ID: %d\n", e2.id);  printf("Employee 2 Name: %s\n", e2.name);  printf("Employee 2 Salary: %f\n", e2.salary);
  return 0;}

Output:

Employee 1 ID: 101Employee 1 Name: Sonoo JaiswalEmployee 1 Salary: 56000.000000Employee 2 ID: 102Employee 2 Name: James BondEmployee 2 Salary: 126000.000000

Detailed Explanation of Structures in C Language


 Popular Recommendations
  • C Language Tutorial – Detailed Explanation of the Math Library

  • C Language Algorithm – “Delete the Nth Node from the End of the Linked List” Algorithm Problem

  • C Language Practice Questions – Programming Practice Questions (19)

Leave a Comment