C Structures

Introduction

In this chapter, we will learn about Structures in C programming. Structures are a user-defined data type in C that allow you to group different types of variables under a single name. This feature is particularly useful for modeling complex data types and organizing related data.

What is a Structure?

A structure in C is a user-defined data type that groups variables of different types into a single unit. Each variable within a structure is called a member. Structures help organize and manage related data more efficiently.

Syntax

The basic syntax for defining a structure in C is as follows:

struct structure_name {
    data_type member1;
    data_type member2;
    ...
    data_type memberN;
};
  • struct: Keyword used to define a structure.
  • structure_name: Name of the structure.
  • data_type: Data type of the structure members.
  • member1, member2, …, memberN: Names of the structure members.

Example: Defining and Using a Structure

Let’s look at a simple example to understand how to define and use a structure.

Example: Defining and Using a Student Structure

#include <stdio.h>

// Defining the structure
struct Student {
    int id;
    char name[50];
    float grade;
};

int main() {
    // Declaring a structure variable
    struct Student student1;

    // Initializing structure members
    student1.id = 1;
    strcpy(student1.name, "Alice");
    student1.grade = 85.5;

    // Accessing and printing structure members
    printf("Student ID: %d\n", student1.id);
    printf("Student Name: %s\n", student1.name);
    printf("Student Grade: %.2f\n", student1.grade);

    return 0; // Returning 0 to indicate successful execution
}

Output:

Student ID: 1
Student Name: Alice
Student Grade: 85.50

In this example, we defined a structure named Student with three members: id, name, and grade. We then declared a variable of type Student, initialized its members, and printed their values.

Accessing Structure Members

Structure members are accessed using the dot operator (.). The syntax for accessing a structure member is as follows:

structure_variable.member

Example: Accessing and Modifying Structure Members

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

// Defining the structure
struct Student {
    int id;
    char name[50];
    float grade;
};

int main() {
    // Declaring and initializing a structure variable
    struct Student student1 = {1, "Alice", 85.5};

    // Accessing and printing structure members
    printf("Student ID: %d\n", student1.id);
    printf("Student Name: %s\n", student1.name);
    printf("Student Grade: %.2f\n", student1.grade);

    // Modifying structure members
    student1.id = 2;
    strcpy(student1.name, "Bob");
    student1.grade = 90.0;

    // Accessing and printing modified structure members
    printf("\nModified Student Information:\n");
    printf("Student ID: %d\n", student1.id);
    printf("Student Name: %s\n", student1.name);
    printf("Student Grade: %.2f\n", student1.grade);

    return 0; // Returning 0 to indicate successful execution
}

Output:

Student ID: 1
Student Name: Alice
Student Grade: 85.50

Modified Student Information:
Student ID: 2
Student Name: Bob
Student Grade: 90.00

Array of Structures

You can create an array of structures to store multiple instances of a structure.

Example: Array of Structures

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

// Defining the structure
struct Student {
    int id;
    char name[50];
    float grade;
};

int main() {
    // Declaring an array of structures
    struct Student students[3];

    // Initializing structure members
    students[0].id = 1;
    strcpy(students[0].name, "Alice");
    students[0].grade = 85.5;

    students[1].id = 2;
    strcpy(students[1].name, "Bob");
    students[1].grade = 90.0;

    students[2].id = 3;
    strcpy(students[2].name, "Charlie");
    students[2].grade = 95.0;

    // Accessing and printing structure members
    for (int i = 0; i < 3; i++) {
        printf("Student ID: %d\n", students[i].id);
        printf("Student Name: %s\n", students[i].name);
        printf("Student Grade: %.2f\n", students[i].grade);
        printf("\n");
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

Student ID: 1
Student Name: Alice
Student Grade: 85.50

Student ID: 2
Student Name: Bob
Student Grade: 90.00

Student ID: 3
Student Name: Charlie
Student Grade: 95.00

In this example, we declared an array of structures named students to store information about three students. We then initialized the structure members and printed their values.

Pointers to Structures

You can also use pointers to structures to access and modify structure members.

Example: Pointers to Structures

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

// Defining the structure
struct Student {
    int id;
    char name[50];
    float grade;
};

int main() {
    // Declaring a structure variable and a pointer to the structure
    struct Student student1 = {1, "Alice", 85.5};
    struct Student *ptr = &student1;

    // Accessing and modifying structure members using the pointer
    printf("Student ID: %d\n", ptr->id);
    printf("Student Name: %s\n", ptr->name);
    printf("Student Grade: %.2f\n", ptr->grade);

    ptr->id = 2;
    strcpy(ptr->name, "Bob");
    ptr->grade = 90.0;

    // Accessing and printing modified structure members using the pointer
    printf("\nModified Student Information:\n");
    printf("Student ID: %d\n", ptr->id);
    printf("Student Name: %s\n", ptr->name);
    printf("Student Grade: %.2f\n", ptr->grade);

    return 0; // Returning 0 to indicate successful execution
}

Output:

Student ID: 1
Student Name: Alice
Student Grade: 85.50

Modified Student Information:
Student ID: 2
Student Name: Bob
Student Grade: 90.00

In this example, we declared a structure variable student1 and a pointer ptr to the structure. We then used the pointer to access and modify the structure members.

Nested Structures

You can define structures within structures to create more complex data types.

Example: Nested Structures

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

// Defining the Address structure
struct Address {
    char street[50];
    char city[50];
    int zip;
};

// Defining the Student structure
struct Student {
    int id;
    char name[50];
    float grade;
    struct Address address; // Nested structure
};

int main() {
    // Declaring a structure variable
    struct Student student1;

    // Initializing structure members
    student1.id = 1;
    strcpy(student1.name, "Alice");
    student1.grade = 85.5;
    strcpy(student1.address.street, "123 Main St");
    strcpy(student1.address.city, "New York");
    student1.address.zip = 10001;

    // Accessing and printing structure members
    printf("Student ID: %d\n", student1.id);
    printf("Student Name: %s\n", student1.name);
    printf("Student Grade: %.2f\n", student1.grade);
    printf("Student Address: %s, %s, %d\n", student1.address.street, student1.address.city, student1.address.zip);

    return 0; // Returning 0 to indicate successful execution
}

Output:

Student ID: 1
Student Name: Alice
Student Grade: 85.50
Student Address: 123 Main St, New York, 10001

In this example, we defined an Address structure and nested it within the Student structure to store address information for the student.

Conclusion

Structures are a powerful feature in C that allow you to group different types of variables under a single name, making it easier to organize and manage related data. By understanding how to define, use, and manipulate structures, you can model complex data types and create more efficient and organized code. Mastering structures is essential for writing robust and maintainable C programs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top