Structure in C

structure

A structure is a collection of different data types that are stored under a common name.

structure in c

Declaration:

The structure in C can be declared in the declarative part of the program, i.e., locally or globally. The structure can be declared by using the reserved word struct.

Syntax: 

struct structure name

{
  datatype structure element 1;
  datatype structure element 2;
  ….
  datatype structure element n;
}

After declaring the structure we have to specify the structure variables.

The syntax of declaring the structure variables is

struct structname(var1, var2, …varn);
struct book
{
  Int pages;
  float price;
  char author[25];
};
struct book b1,b2;
the another way of declaring the structure is
struct book
{ 
 Int pages;
  float price;
  char author[25];
}b1,b2;

Initializing the structure:

Like ordinary variables, we can also initialize the structure variables.

struct book
{
  Int pages;
  float price;
  char author[30];
}b1={200, 43.56, “rama”};
 b2={400, 30.56, “Krishna”};
scanf(“%d%f%s”, &b3.pages, &b3.prices, &b3.author);
}

Accessing elements of a structure:

The elements of a structure are also called members of a structure. The members of a structure can be accessed by using a special operator called as (.) operator.

Syntax

structure variable. element of structure

Ex: b1.pages b2.author    b3.price

C program to create a structured student with members as student number, name and marks. Initialize the values and display them.

#include <stdio.h>  // Include the standard input/output library
#include <string.h> // Include the string library for string manipulation
// Define the student structure
struct student {
    int student_number;
    char name[50];    // Assuming a maximum name length of 49 characters + null terminator
    float marks;
};
int main() {
    // Declare and initialize a student structure variable
    struct student s1 = {12345, "John Doe", 85.5}; 
    
    // Display the student's information
    printf("Student Number: %d\n", s1.student_number);
    printf("Name: %s\n", s1.name);
    printf("Marks: %.2f\n", s1.marks); // Display marks with 2 decimal places
    return 0; // Indicate successful program execution
}

Output:

Student Number: 12345
Name: John Doe
Marks: 85.50

Nested structures

A structure that is included in another structure is called a nested structure. We can take the members of one structure as a member of another structure. 

Nested structures are used to solve complex data applications.

C Program using Nested Structures

#include <stdio.h>
#include <string.h>
// Define a nested structure for the date of birth
struct dateOfBirth {
    int day;
    int month;
    int year;
};
// Define the main student structure with the nested dateOfBirth structure
struct student {
    int student_number;
    char name[50];
    float marks;
    struct dateOfBirth dob; // Nested structure for date of birth
};
int main() {
    // Declare and initialize a student structure variable
    struct student s1 = {
        12345, "John Doe", 85.5, {15, 8, 2000} // Initialize DOB
    };
    // Display student information (including date of birth)
    printf("Student Number: %d\n", s1.student_number);
    printf("Name: %s\n", s1.name);
    printf("Marks: %.2f\n", s1.marks);
    printf("Date of Birth: %d/%d/%d\n", s1.dob.day, s1.dob.month, s1.dob.year);
    return 0;
}

Output:

Student Number: 12345
Name: John Doe
Marks: 85.50
Date of Birth: 15/8/2000

Array of structures

In an array of structures each element of an array will be a structure variable. A structure variable is a collection of different data types that are stored under a common name.

Array of structures can be declared as

struct book
{
 Int pages;
float price;
char author[30];
}b=[10];

C Program using Array Structure

#include <stdio.h>
#include <string.h>
struct dateOfBirth {
    int day;
    int month;
    int year;
};
struct student {
    int student_number;
    char name[50];
    float marks;
    struct dateOfBirth dob;
};
int main() {
    // Declare an array of 3 student structures
    struct student students[3];
    // Initialize the student data
    students[0] = (struct student){12345, "John Doe", 85.5, {15, 8, 2000}};
    students[1] = (struct student){54321, "Jane Smith", 92.0, {3, 12, 1999}};
    students[2] = (struct student){98765, "Michael Johnson", 78.3, {22, 4, 2001}};
    // Display information for each student in the array
    printf("\nStudent Information:\n");
    for (int i = 0; i < 3; i++) {
        printf("Student %d:\n", i + 1);
        printf("  Number: %d\n", students[i].student_number);
        printf("  Name: %s\n", students[i].name);
        printf("  Marks: %.2f\n", students[i].marks);
        printf("  Date of Birth: %d/%d/%d\n", 
               students[i].dob.day, students[i].dob.month, students[i].dob.year);
    }
    return 0;
}

Output:

Student Information:
Student 1:
  Number: 12345
  Name: John Doe
  Marks: 85.50
  Date of Birth: 15/8/2000
Student 2:
  Number: 54321
  Name: Jane Smith
  Marks: 92.00
  Date of Birth: 3/12/1999
Student 3:
  Number: 98765
  Name: Michael Johnson
  Marks: 78.30
  Date of Birth: 22/4/2001

Self-referential Structure

A self-referential structure is a structure that contains at least one member as a pointer pointing to the same structure. Generally, it is used in linked lists. A linked list is a collection of nodes. Every node contains two parts such as data and link. Data consists of the value that we want to store. The link is going to contain the address of the next node.

self-referential-structure

The node in C language can be declared by self-referential structure.

struct node
{
 Int data;
 struct node*link;
}

C Program using Self-referential Structure

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define a nested structure for the date of birth
struct dateOfBirth {
    int day;
    int month;
    int year;
};
// Self-referential structure for student information
struct student {
    int student_number;
    char name[50];
    float marks;
    struct dateOfBirth dob;
    struct student *next; // Pointer to the next student in the list
};
int main() {
    // Create three student nodes
    struct student *head = NULL;   // Head of the list (initially empty)
    struct student *second = NULL;
    struct student *third = NULL;
    // Allocate memory for the nodes
    head = (struct student *)malloc(sizeof(struct student));
    second = (struct student *)malloc(sizeof(struct student));
    third = (struct student *)malloc(sizeof(struct student));
    // Populate the student information
    head->student_number = 12345;
    strcpy(head->name, "John Doe");
    head->marks = 85.5;
    head->dob = (struct dateOfBirth){15, 8, 2000};
    head->next = second;  // Link head to second
    second->student_number = 54321;
    strcpy(second->name, "Jane Smith");
    second->marks = 92.0;
    second->dob = (struct dateOfBirth){3, 12, 1999};
    second->next = third;  // Link second to third
    third->student_number = 98765;
    strcpy(third->name, "Michael Johnson");
    third->marks = 78.3;
    third->dob = (struct dateOfBirth){22, 4, 2001};
    third->next = NULL;   // Last node in the list
    // Traverse and print the linked list of students
    printf("\nStudent Information:\n");
    struct student *temp = head; 
    while (temp != NULL) {
        printf("  Number: %d\n", temp->student_number);
        printf("  Name: %s\n", temp->name);
        printf("  Marks: %.2f\n", temp->marks);
        printf("  Date of Birth: %d/%d/%d\n", temp->dob.day, temp->dob.month, temp->dob.year);
        printf("\n");
        temp = temp->next; // Move to the next node
    }
    
    // Free the allocated memory to prevent memory leaks
    free(head);
    free(second);
    free(third);
    return 0;
}

Output:

Student Information:
  Number: 12345
  Name: John Doe
  Marks: 85.50
  Date of Birth: 15/8/2000
  Number: 54321
  Name: Jane Smith
  Marks: 92.00
  Date of Birth: 3/12/1999
  Number: 98765
  Name: Michael Johnson
  Marks: 78.30
  Date of Birth: 22/4/2001

FAQs of Structure in C

1. What is a structure in C, and why are they used?

  • A structure is a user-defined data type that groups together related variables of different data types under a single name. 
  • This allows you to organize complex data in a meaningful way, making your code easier to read, maintain, and manage.
    • For example, a student structure might group together a student’s name, student_number, marks, and dateOfBirth.

2. How do you declare and define a structure in C?

Declaration:

struct student {  

    int student_number;

    char name[50];

    float marks; 

};

Definition:

struct student s1; // Creates a structure variable ‘s1’

3. How do you access members of a structure?

  • Answer: You use the dot (.) operator to access individual members of a structure.

s1.student_number = 12345; 

printf(“Name: %s\n”, s1.name);

4. What is the difference between a structure and an array in C?

  • Structures: Group related data of different data types.
  • Arrays: Store a collection of elements of the same data type.

5. Can a structure contain a pointer to itself (self-referential structure)?

  • Yes! A self-referential structure contains a pointer member that points to the same structure type. This is the fundamental building block for creating linked lists and other dynamic data structures in C.

Example:

struct node {

    int data;

    struct node *next; // Pointer to the next node

};

Scroll to Top