Lesson 48 of 60 – Structures in C
80%

Structures in C

A structure in C is a user-defined data type that allows us to group different types of data under one name. Structures are useful when we need to represent a real-world object such as a student, employee, product, book, or customer.

Note: Unlike an array, which normally stores elements of the same type, a structure can contain members of different data types.

1. What is a Structure?

A structure is a user-defined data type that groups related variables of different data types together.

struct Student
{
    int id;
    char name[50];
    float marks;
};

Here, Student contains an integer, a character array, and a floating-point value.

2. Why Use Structures?

Structures are useful for representing records containing related information.

  • Student records
  • Employee records
  • Product details
  • Book information
  • Customer information
  • Bank account records

3. Structure Syntax

The general syntax for defining a structure is:

struct StructureName
{
    data_type member1;
    data_type member2;
    data_type member3;
};

The semicolon after the closing brace is required.

4. Defining a Student Structure

struct Student
{
    int id;
    char name[50];
    float marks;
};

This defines a structure type named struct Student. It does not by itself create a variable named student.

5. Declaring a Structure Variable

After defining a structure, we can declare variables using the structure type.

struct Student student1;

Here, student1 is a structure variable.

6. Accessing Structure Members

The dot operator . is used to access members of a structure variable.

student1.id = 101;
student1.marks = 85.5;

Here, id and marks are members of student1.

7. Structure Initialization

A structure variable can be initialized when it is declared.

struct Student student1 = {
    101,
    "Rahul",
    85.5
};

The values are assigned to the members in the order in which they are declared.

8. Designated Initializers

C also supports designated initializers, which allow members to be initialized by name.

struct Student student1 = {
    .id = 101,
    .name = "Rahul",
    .marks = 85.5
};

This can make initialization clearer.

9. Printing Structure Members

#include <stdio.h>

struct Student
{
    int id;
    char name[50];
    float marks;
};

int main()
{
    struct Student student1 = {101, "Rahul", 85.5};

    printf("ID: %d\n", student1.id);
    printf("Name: %s\n", student1.name);
    printf("Marks: %.2f\n", student1.marks);

    return 0;
}

10. Taking Structure Input

Structure members can be filled using scanf() and other input functions.

struct Student student1;

printf("Enter ID: ");
scanf("%d", &student1.id);

printf("Enter marks: ");
scanf("%f", &student1.marks);

11. Taking String Input in a Structure

A character array inside a structure can be read using fgets().

printf("Enter name: ");
fgets(student1.name, sizeof(student1.name), stdin);

When using fgets(), remember that it may store the newline if the input fits in the buffer.

12. Structure with Different Data Types

A structure can contain many different data types.

struct Employee
{
    int id;
    char name[50];
    float salary;
    char grade;
};

This allows related employee information to be stored together.

13. Multiple Structure Variables

Multiple variables of the same structure type can be declared.

struct Student student1;
struct Student student2;
struct Student student3;

Each variable has its own copy of the structure members.

14. Structure Assignment

Two structure variables of the same structure type can be assigned to each other.

struct Student student1 = {101, "Rahul", 85.5};
struct Student student2;

student2 = student1;

The members of student1 are copied to student2.

15. Array of Structures

An array can contain multiple structure variables.

struct Student students[3];

Each element of the array is a complete struct Student object.

students[0].id = 101;
students[1].id = 102;
students[2].id = 103;

16. Displaying an Array of Structures

for(int i = 0; i < 3; i++)
{
    printf("ID: %d\n", students[i].id);
    printf("Name: %s\n", students[i].name);
    printf("Marks: %.2f\n", students[i].marks);
}

A loop can process every structure in the array.

17. Nested Structures

A structure can contain another structure as a member.

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

struct Student
{
    int id;
    char name[50];
    struct Date admissionDate;
};

18. Accessing Nested Structure Members

Use multiple dot operators to access a nested structure member.

student1.admissionDate.day = 15;
student1.admissionDate.month = 8;
student1.admissionDate.year = 2026;

19. Pointer to Structure

A pointer can store the address of a structure variable.

struct Student student1;

struct Student *ptr = &student1;

The pointer can be used to access the structure.

20. Arrow Operator

The arrow operator -> is used to access structure members through a pointer.

ptr->id = 101;
ptr->marks = 90.5;

The expression ptr->id is equivalent to (*ptr).id.

21. Passing Structure to a Function

A structure can be passed to a function by value.

void display(struct Student s)
{
    printf("ID: %d\n", s.id);
    printf("Name: %s\n", s.name);
    printf("Marks: %.2f\n", s.marks);
}

The function receives a copy of the structure.

22. Passing Structure Using a Pointer

A pointer to a structure can be passed to a function.

void display(struct Student *s)
{
    printf("ID: %d\n", s->id);
    printf("Name: %s\n", s->name);
    printf("Marks: %.2f\n", s->marks);
}

This avoids copying the whole structure and allows the function to modify the original structure when the pointer is not const.

23. Modifying a Structure Through a Pointer

void updateMarks(struct Student *s)
{
    s->marks = 95.0;
}

int main()
{
    struct Student student1 = {101, "Rahul", 85.0};

    updateMarks(&student1);

    printf("%.2f", student1.marks);

    return 0;
}

24. typedef with Structure

The typedef keyword can create a shorter name for a structure type.

typedef struct
{
    int id;
    char name[50];
    float marks;
} Student;

Now we can declare:

Student student1;

instead of writing struct Student.

25. Structure and const Pointer

A function can receive a pointer to a constant structure when it only needs to read the structure.

void display(const struct Student *s)
{
    printf("%d\n", s->id);
    printf("%s\n", s->name);
    printf("%.2f\n", s->marks);
}

The function cannot modify the structure through s.

26. Common Mistake: Missing Semicolon

A structure definition must end with a semicolon.

struct Student
{
    int id;
    char name[50];
};

Forgetting the final semicolon causes a compilation error.

27. Common Mistake: Wrong Member Access

Use the dot operator with a structure variable and the arrow operator with a pointer to a structure.

struct Student student1;
struct Student *ptr = &student1;

student1.id = 101;
ptr->id = 102;

Using the wrong operator can result in a compilation error.

28. Complete Student Structure Program

#include <stdio.h>

struct Student
{
    int id;
    char name[50];
    float marks;
};

void display(struct Student *s)
{
    printf("\nStudent Details\n");
    printf("ID: %d\n", s->id);
    printf("Name: %s\n", s->name);
    printf("Marks: %.2f\n", s->marks);
}

int main()
{
    struct Student student1;

    printf("Enter ID: ");
    scanf("%d", &student1.id);

    printf("Enter name: ");
    scanf("%49s", student1.name);

    printf("Enter marks: ");
    scanf("%f", &student1.marks);

    display(&student1);

    return 0;
}

This program creates a structure, takes input, and passes the structure to a function using a pointer.

29. Practical Uses of Structures

Structures are widely used for organizing related information.

  • Student management systems
  • Employee management systems
  • Library management systems
  • Banking applications
  • Product management
  • Customer records
  • Book records
  • Inventory systems
  • Data structures such as linked lists

30. Practice Program

Create a C program that:

  1. Defines an Employee structure.
  2. Stores employee ID, name, department, and salary.
  3. Takes input from the user.
  4. Displays the employee information.
  5. Creates an array of 5 employees.
  6. Displays all employees using a loop.
  7. Uses a pointer to access an employee structure.
  8. Creates a function to display employee details.

Try using both the dot operator . and arrow operator ->.

📌 Key Points

  • A structure is a user-defined data type.
  • A structure can contain members of different data types.
  • The . operator accesses members through a structure variable.
  • The -> operator accesses members through a structure pointer.
  • Structures can be initialized when declared.
  • Multiple variables of the same structure type can be created.
  • Arrays of structures can store multiple records.
  • Structures can contain other structures.
  • Structures can be passed to functions by value or through pointers.
  • A pointer to a structure can modify the original structure.
  • typedef can provide a shorter name for a structure type.
  • Structures are useful for representing real-world records.

🧠 Quick Quiz

Question: Which operator is used to access a structure member through a structure pointer?