Structures (struct) Introduction In C

The struct is a keyword in C which is derived from “structures”. it is a user-defined data type that allows you to group different data types (like integers, floats, and characters) under a single name.

Importance: Structs are incredibly useful because they allow you to create more complex data models that more closely represent objects in the real world. For example, if you want to store information about a book, you can create a struct that contains a title, author, and number of pages.  Here’s a descriptive diagram :

Structures (struct) In C - introduction

Important: A struct in C contains heterogeneous fields grouped together in contiguous memory.

Creating Struct Instance / Variables

Once you’ve defined a struct, you can create instances (also known as variables) of that struct type. Each instance represents a separate copy of the struct, allowing you to store unique values for each instance. There are two major methods are used to create the instance of a a struct

Method 01: Declare structure variables at the time of defining the structure.
Method 02: Define the structure first and then declare the structure instances (variables) later inside the main() function.

Structures (struct) declaration methods In C

In the above diagram, the std1 and std2 are instances of struct, through these instances, you can access all values of student.

 

Initializing Structs

You can initialize structs after declaration. C requires that all initialization happen outside the structure definition. 

Invalid Method: Cause Error because initialization happens inside the struct.

struct student{
                int RollNo= 10;           // ❌ Error: Initialization inside struct definition                 float marks = 112.5;            // ❌ Error: Initialization inside struct definition                  char name[50] = {"Brian and Dennis"};  // ❌ Error: Initialization inside struct definition
};

Valid Method 01: pass all values in the main function

void main {
                 struct student std1 = {10, 112.5, "Brian and Dennis"}; 
}

Valid Method 02: pass values One by one 

void main {

std1.RollNo = 10; 
std1.marks= 112.5; 
 strcpy(std1.name, "Brian and Dennis");
}

Note: strcpy is a built-in function to put string value 

Accessing Structs Elements

Method 01: When you have a normal structure variable (not a pointer) then we use  dot ( . ) operator. For example, to print the title of book1, you would write:

printf(“Student Name: %s”, std1.name);

Method 02: When you have a pointer to a structure then we use arrow (–>) operator as following
                   

struct student *ptr = &std1; // Pointer to the structure , Access structure members using arrow operator 
printf("Name: %s\n", ptr->name);

Example Program Using structs

Here’s a complete example that demonstrates creating a struct, initializing it, and accessing its members:

#include <stdio.h>
#include <string.h> // Required for strcpy if using Method 2

struct student {
int RollNo;
float marks;
char name[12]; 
};

int main() {

  // Method 01: Assign all values at once
  struct student std1= {101, 95.5, "Alice"};
  printf("Student RollNo: %d\n", std1.RollNo);
  printf("Marks: %.2f\n", std1.marks);
  printf("Name: %s\n", std1.name);

// Method 02: Assign values one by one
  struct student std2;
  std2.RollNo = 102;
  std2.marks = 89.0;
  strcpy(std2.name, "Bob");

  printf("Student RollNo: %d\n", std2.RollNo);
  printf("Marks: %.2f\n", std2.marks);
  printf("Name: %s\n", std2.name);

return 0;
}
Output:
Student RollNo: 101 
Marks: 95.50
 Name: Alice 
Student RollNo: 102 
Marks: 89.00 
Name: Bob

This program defines a student struct, creates std1 and std2 variables (instances), initializes it with data, and prints out the students’ details.

Important:  structure can be pointer structure, self-referential structure, or nested structure. a simple structure of these is given below, for more details visit the links in this paragraph. 

1. Pointer Structure

A structure can have a pointer to itself or another structure.

struct Employee { 
int id; 
char name[20]; 
float salary; 
struct Employee *manager; // Pointer to another Employee structure 
};

2. Self-Referential Structure

A self-referential structure contains a pointer to itself. This is commonly used in linked lists, trees, etc.

struct Node { 
int data; 
struct Node *next; // Pointer to the next node
 };

Example:3. Nested Structure

A structure can contain another structure as a member.

struct student  
{     
   int RollNo;  
   char name[12];  
   struct Date  
    {  
      int dd;  
      int mm;  
      int yyyy;   
    }dob;  
}std1;