C Programming

Structure in C

Structure is an important concept in C. There are two types of data types that exist in C. They are primitive data types and non-primitive or User-defined data types. The structure is an example of a non-primitive data type.

What is Structure

Structure is a way to group variables. Sometimes, we have to store some information in a group.

Suppose we have to store 100 students’ information. Information means their name, address, mobile no, gender, marks, aggregates, etc. In this situation, if we store these data to the multiple variables for each student, the program becomes very complex, and it increases the execution speed of the program.

To reduce this problem, we introduce Structure. With the help of structure, we can able to group these variables of each student into a single variable.

Structure is a collection of dissimilar elements. Dissimilar means we can store different data type values like int, char, float, string, etc.

If we store a student’s information, we have to store his or her name, marks, mobile no, etc. Name is a string; a mark is a float; mobile no is an integer array. So it is a group of dissimilar elements.

Defining Structure means creating a new data type, and we know that creating a new data type means we use here non-primitive data type.

How to Define a Structure

struct date           // defining a structure ;
  {
     int d, m ,y ;
};

struct student       // defining another structure ;
   {
      int roll_no ;
      char name [ 30 ] ;
       int age ;
}

The above-mentioned date and student are new data types called user-defined data types. This type of data type has to be defined in our program. Because primitive data type is defined in C, but non-primitive data type is not defined in C, it has to be defined by the user. For this, we have to write a keyword called struct before the name of the user-defined data type.

Memory Consumption

No memory is consumed for a definition of structure. Because memory is consumed for the declaration of a variable but not in the case of defining data type.

Defining structure means creating a new data type; memory is not consumed. Memory is consumed at the time of declaration of a particular type of variable

Where Can We Define Structure

We can define a structure as locally or globally depending on the requirement of the program.

How Do You Create Variables Through Structure

Programming Example1:

#include<stdio.h>

struct date         // defining a structureglobally ;
 {
    int d, m, y ;
};
struct date d1 ;

int main()
 {
   d1.d = 24 ;         // Assigning values to the member variables of d1 ;
   d1.m = 4 ;
    d1.y = 2022 ;
    printf ( “ Date is : %d %d %d “ , d1.d , d1.m , d1.y ) ;
retun0 ;
}

Output

Explanation

Here we create a date data type globally. To define a date data type, we use keyword struct. Now we declare a variable d1 with no initialization. Inside the main function, we assign the values of the member variable of date; they are d1.d, d1.m, d1.y. Now we print the values.

Programming Example 2

#include<stdio.h>

struct date         // defining a structure globally ;
 {
    int d, m, y ;
};

int main()
 {
   struct date d1 = { 24, 4, 2022} ;
 // Declaration and assignment of values locally ;
    printf ( “ Date is : %d %d %d “ , d1.d , d1.m , d1.y ) ;
    return 0 ;
}

Output

Explanation

Here we define date data type globally. Inside the main () function, we declare and assign a variable d1 of the date data type. So this variable d1 acts like a local variable. Here the values automatically go to the member variable of the date. Now we print the date.

Programming Example 3

#include<stdio.h>

struct date         // defining a structure globally ;
 {
    int d, m, y ;
};
struct date d1 ;

int main()
 {
printf (“ Enter a Date :) ;
scanf(%d/ %d/ %d ” , &d1.d, &d1.y , &d1.y) ;  // entering date from      user ;
    printf ( “ Date is : %d %d %d “ , d1.d , d1.m , d1.y ) ;
   return 0 ;
}

Output

Self Referential Structure

Self Referential Structure is also a structure. It is a structure with pointers that points to the same type of structure as a member. We show an example below.

Programming Example 4

#include<stdio.h>

int main()
 {
struct node
     {
       int data ;
       struct node * link ;
     };
struct node n1 ;
n1. data = 5 ;
n1 . link = NULL ;
struct node n2 ;
n2. Data = 7 ;
n2. Link = NULL ;
n1. Link = & n2 ;       // The address of n2 is assigned to link part of n1.
printf ( “ The value of data is : %d ”, n1. data ) ;
printf ( “ The value of link is : %d ”, n1. link ->data ) ;

return 0 ;
}

Output

Explanation

The above program is an example of Self Referential Structure. Here we create a user-defined data type called a node. It has two data members inside its body. One is data; another one is linked. Link is a pointer which type is node type. It means it can hold the address of node type data only. Now we create a variable n1 which is a node type data type. Here we assign an int type value 5. In the linked part, we assign NULL.

Another variable, b, is also a node type data type. 7 and NULL are stored in their respective data members. The important aspect is that we store the address of b to the link of a. It means the link of a variable now points to the b variable. It is called Self Referential structure.

Application of Self Referential Structure

Self Referential Structure is vastly used in the field of Link List, Trees.

Conclusion

From the concept of Structure, we have concluded that the requirement of structure in C is very important. Through structure, we can create our own data type according to the requirement of the program. Through structure, we can store different types of metadata.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com