C Structures (CT 101): Complete IOE Notes with Examples
A diagram illustrating the concept of C Structures and Pointers
Structure
5 hours | 6 marks

COMPUTER PROGRAMMING (CT 101) – Chapter 8

A Structure in C is a powerful user-defined data type that allows you to group together variables of different data types under a single name. This is incredibly useful for organizing complex data, such as records for a student, employee, or book, making your code cleaner and more logical.

Chapter Information

Chapter 8: Structure (5 hours) – 6 marks

Course: Computer Programming (CT 101), I Year I Part

Description: This guide provides a complete overview of C Structures, covering definition, declaration, arrays of structures, nested structures, and pointers to structures, as per the IOE syllabus.

Credit: Sujan Karki

Detailed Chapter Notes

8.1 Defining, Declaring, Accessing & Initializing Structure Elements

Defining a Structure

You define a structure using the struct keyword. This creates a template but does not allocate memory.

struct student {
    char name[50];
    int roll;
    float marks;
};

Declaring Structure Variables

Once defined, you can create variables of that structure type.

// Method 1: After definition
struct student s1, s2;

// Method 2: With definition
struct employee {
    char name[50];
    int id;
} emp1;

Initializing a Structure

You can initialize a structure’s members at the time of declaration.

struct student s1 = {"John Doe", 101, 85.5};

Accessing Structure Members

Use the dot (.) operator to access individual members of a structure variable.

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

struct student {
    char name[50];
    int roll;
    float marks;
};

int main() {
    struct student s1;
    strcpy(s1.name, "Alice");
    s1.roll = 102;
    s1.marks = 92.0;

    printf("Student Name: %s\n", s1.name);
    printf("Roll Number: %d\n", s1.roll);
    printf("Marks: %.1f\n", s1.marks);
    
    return 0;
}

8.2 Array of Structure

An array of structures is used to store multiple records. Each element of the array is a complete structure.

#include <stdio.h>

struct student {
    char name[50];
    int roll;
};

int main() {
    struct student class[3]; // Array of 3 student structures
    int i;

    // Reading data for 3 students
    for (i = 0; i < 3; i++) {
        printf("\nEnter details for student %d:\n", i + 1);
        printf("Enter name: ");
        scanf("%s", class[i].name);
        printf("Enter roll number: ");
        scanf("%d", &class[i].roll);
    }

    // Displaying data
    printf("\nStudent Information:\n");
    for (i = 0; i < 3; i++) {
        printf("Name: %s, Roll: %d\n", class[i].name, class[i].roll);
    }

    return 0;
}

8.3 Array within Structure

A member of a structure can itself be an array. This is commonly used for strings (character arrays) or to hold multiple values like test scores for a single student.

struct student {
    char name[50]; // Array within structure
    int roll;
    float test_scores[4]; // Array within structure
};

8.4 Structure within Structure (Nested Structure)

You can define a structure as a member of another structure. This is called nesting.

#include <stdio.h>

struct Date {
    int day;
    int month;
    int year;
};

struct Employee {
    char name[50];
    struct Date dob; // Nested structure
};

int main() {
    struct Employee emp1 = {"Bob", {15, 6, 1995}};

    printf("Employee Name: %s\n", emp1.name);
    printf("Date of Birth: %d/%d/%d\n", emp1.dob.day, emp1.dob.month, emp1.dob.year);
    
    return 0;
}

8.5 Structure & Pointer

A pointer can point to a structure variable. To access members of a structure using a pointer, you use the arrow (->) operator instead of the dot operator.

#include <stdio.h>

struct student {
    char name[50];
    int roll;
};

int main() {
    struct student s1 = {"Charlie", 103};
    struct student *ptr; // Pointer to a structure
    
    ptr = &s1; // Store the address of s1 in ptr

    printf("Name using pointer: %s\n", ptr->name);
    printf("Roll using pointer: %d\n", ptr->roll);

    return 0;
}

The expression ptr->name is a convenient shorthand for (*ptr).name.

8.6 Passing Structures to/from Function

Structures can be passed to functions in three main ways:

  1. Passing individual members (by value): Simple but inefficient if you need to pass many members.
  2. Passing the entire structure (by value): A copy of the structure is created. The original structure is not modified. Easy to implement but can be slow for large structures.
  3. Passing a pointer to the structure (by reference): The address of the structure is passed. This is the most efficient method as no copy is made. The function can modify the original structure's data.

Example: Passing Structure by Reference (Pointer)

#include <stdio.h>

struct student {
    char name[50];
    int roll;
};

// Function accepts a pointer to a student structure
void displayStudent(struct student *st) {
    printf("Name: %s\n", st->name);
    printf("Roll: %d\n", st->roll);
}

int main() {
    struct student s1 = {"David", 104};
    
    // Pass the address of the structure
    displayStudent(&s1); 
    
    return 0;
}

PDF Notes

×

Disclaimer

The educational materials provided on this website are intended as supplementary resources to support your learning journey. These study materials are sample documents designed to help students understand complex concepts in C Programming.

We have made every effort to ensure the accuracy of the content. However, we recommend students to refer to standard textbooks and consult with professors for authoritative explanations. These materials should be used as references only.

Scroll to Top