Nested Structures and Structure Pointers in C Practice Questions with Solutions

Introduction

Nested structures allow one structure to contain another structure as a member. Structure pointers allow us to store the address of a structure and access its members using the -> operator. These concepts are important when working with larger records such as students, employees, customers, and addresses. In this chapter, you will practice nested structures, structure pointers, accessing nested members, passing structures to functions, and modifying structure data through pointers. Nested Structures and Structure Pointers in C Practice questions with solutions to help you understand the concepts.

Q1. Create a Simple Nested Structure

Problem Statement

Create a Student structure containing a nested Address structure. Store and display the student’s name, city, and PIN code.

C Program

#include <stdio.h>

struct Address
{
    char city[50];
    int pin;
};

struct Student
{
    char name[50];
    struct Address address;
};

int main()
{
    struct Student student = {
        "Rahul",
        {"Delhi", 110075}
    };

    printf("Name = %s\n", student.name);
    printf("City = %s\n", student.address.city);
    printf("PIN Code = %d", student.address.pin);

    return 0;
}

Sample Output

Name = Rahul
City = Delhi
PIN Code = 110075

Explanation

First, we create the Address structure:

struct Address
{
    char city[50];
    int pin;
};

Then we use it inside the Student structure:

struct Student
{
    char name[50];
    struct Address address;
};

Now address becomes a member of Student.

We access the nested members using two dot operators:

student.address.city
student.address.pin

The pattern is:

student
   ↓
address
   ↓
city / pin

Concepts Covered

  • Nested structures
  • Structure members
  • Dot . operator
  • Structure initialization

Q2. Take Input for a Nested Structure

Problem Statement

Create a student structure containing a nested address structure. Take the student’s name, city, and PIN code as input.

C Program

#include <stdio.h>

struct Address
{
    char city[50];
    int pin;
};

struct Student
{
    char name[50];
    struct Address address;
};

int main()
{
    struct Student student;

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

    printf("Enter city: ");
    scanf("%49s", student.address.city);

    printf("Enter PIN code: ");
    scanf("%d", &student.address.pin);

    printf("\nStudent Details\n");
    printf("Name = %s\n", student.name);
    printf("City = %s\n", student.address.city);
    printf("PIN Code = %d", student.address.pin);

    return 0;
}

Sample Output

Enter student name: Rahul
Enter city: Delhi
Enter PIN code: 110075

Student Details
Name = Rahul
City = Delhi
PIN Code = 110075

Explanation

The nested structure is accessed like this:

student.address.city

For the PIN code:

student.address.pin

Notice how the dot operator is used twice:

student → address → city

This is the basic idea behind nested structures.

Concepts Covered

  • Nested structure input
  • Multiple dot operators
  • Structure members
  • scanf()

Q3. Create a Structure Pointer

Problem Statement

Create an employee structure and use a pointer to access its members.

C Program

#include <stdio.h>

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

int main()
{
    struct Employee employee = {101, "Amit", 35000};

    struct Employee *ptr;

    ptr = &employee;

    printf("ID = %d\n", ptr->id);
    printf("Name = %s\n", ptr->name);
    printf("Salary = %.2f", ptr->salary);

    return 0;
}

Sample Output

ID = 101
Name = Amit
Salary = 35000.00

Explanation

We create a structure variable:

struct Employee employee;

Then create a structure pointer:

struct Employee *ptr;

We store the address of employee:

ptr = &employee;

Because ptr points to a structure, we use -> to access its members:

ptr->id
ptr->name
ptr->salary

You can think of:

ptr->id

as another way of writing:

(*ptr).id

Concepts Covered

  • Structure pointer
  • Address operator &
  • Arrow operator ->
  • Dereferencing

Q4. Modify Structure Members Using a Pointer

Problem Statement

Create a structure for an employee and change the employee’s salary using a structure pointer.

C Program

#include <stdio.h>

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

int main()
{
    struct Employee employee = {101, "Amit", 30000};

    struct Employee *ptr = &employee;

    printf("Before Update = %.2f\n", ptr->salary);

    ptr->salary = 40000;

    printf("After Update = %.2f", ptr->salary);

    return 0;
}

Sample Output

Before Update = 30000.00
After Update = 40000.00

Explanation

The pointer points to employee:

struct Employee *ptr = &employee;

We can modify the original structure member:

ptr->salary = 40000;

Because ptr contains the address of employee, the original employee.salary is also changed.

Concepts Covered

  • Structure pointer
  • Modifying structure members
  • -> operator
  • Address of structure

Q5. Access Nested Structure Using a Structure Pointer

Problem Statement

Create a student structure with a nested address structure. Use a structure pointer to display the student’s city and PIN code.

C Program

#include <stdio.h>

struct Address
{
    char city[50];
    int pin;
};

struct Student
{
    char name[50];
    struct Address address;
};

int main()
{
    struct Student student = {
        "Priya",
        {"Mumbai", 400001}
    };

    struct Student *ptr = &student;

    printf("Name = %s\n", ptr->name);
    printf("City = %s\n", ptr->address.city);
    printf("PIN Code = %d", ptr->address.pin);

    return 0;
}

Sample Output

Name = Priya
City = Mumbai
PIN Code = 400001

Explanation

Here:

ptr

points to the complete Student structure.

So we first use:

ptr->address

to access the nested Address structure.

Then we use:

ptr->address.city
ptr->address.pin

The access path is:

ptr
 ↓
Student
 ↓
address
 ↓
city / pin

Concepts Covered

  • Nested structures
  • Structure pointers
  • Arrow operator
  • Nested member access

Q6. Pass a Structure Pointer to a Function

Problem Statement

Create a student structure and pass its address to a function. Display the student’s details inside the function.

C Program

#include <stdio.h>

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

void displayStudent(struct Student *student)
{
    printf("Roll = %d\n", student->roll);
    printf("Name = %s\n", student->name);
    printf("Marks = %.2f", student->marks);
}

int main()
{
    struct Student student = {101, "Rahul", 88.5};

    displayStudent(&student);

    return 0;
}

Sample Output

Roll = 101
Name = Rahul
Marks = 88.50

Explanation

The function receives a structure pointer:

void displayStudent(struct Student *student)

We pass the address:

displayStudent(&student);

Inside the function, we use:

student->roll
student->name
student->marks

This avoids passing a separate copy of the complete structure to the function.

Concepts Covered

  • Structure pointer
  • Function arguments
  • Passing structure address
  • -> operator

Q7. Change Structure Data Inside a Function

Problem Statement

Create an employee structure and use a function with a structure pointer to increase the employee’s salary.

C Program

#include <stdio.h>

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

void increaseSalary(struct Employee *employee)
{
    employee->salary = employee->salary + 5000;
}

int main()
{
    struct Employee employee = {101, "Aman", 30000};

    printf("Before Increase = %.2f\n", employee.salary);

    increaseSalary(&employee);

    printf("After Increase = %.2f", employee.salary);

    return 0;
}

Sample Output

Before Increase = 30000.00
After Increase = 35000.00

Explanation

We pass the address:

increaseSalary(&employee);

The function receives it as:

struct Employee *employee

Then:

employee->salary

accesses the original salary.

The function changes it:

employee->salary = employee->salary + 5000;

The change is visible in main() because the function is working with the original structure through its address.

Concepts Covered

  • Structure pointer
  • Function with pointer parameter
  • Modifying original structure
  • -> operator

Q8. Nested Structure for Employee Address

Problem Statement

Create an employee structure containing a nested address structure. Take employee details and display the complete record.

C Program

#include <stdio.h>

struct Address
{
    char city[50];
    char state[50];
    int pin;
};

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

int main()
{
    struct Employee employee;

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

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

    printf("Enter salary: ");
    scanf("%f", &employee.salary);

    printf("Enter city: ");
    scanf("%49s", employee.address.city);

    printf("Enter state: ");
    scanf("%49s", employee.address.state);

    printf("Enter PIN code: ");
    scanf("%d", &employee.address.pin);

    printf("\nEmployee Details\n");
    printf("ID = %d\n", employee.id);
    printf("Name = %s\n", employee.name);
    printf("Salary = %.2f\n", employee.salary);
    printf("City = %s\n", employee.address.city);
    printf("State = %s\n", employee.address.state);
    printf("PIN = %d", employee.address.pin);

    return 0;
}

Sample Output

Enter employee ID: 201
Enter employee name: Amit
Enter salary: 45000
Enter city: Delhi
Enter state: Delhi
Enter PIN code: 110075

Employee Details
ID = 201
Name = Amit
Salary = 45000.00
City = Delhi
State = Delhi
PIN = 110075

Explanation

The Employee structure contains another structure:

struct Address address;

Therefore, the employee’s city is accessed using:

employee.address.city

The state:

employee.address.state

And PIN:

employee.address.pin

This is useful for representing real-world records where one object contains another related object.

Concepts Covered

  • Nested structures
  • User input
  • Structure member access
  • Real-world record design

Q9. Use a Pointer with an Array of Structures

Problem Statement

Create an array of three students and use a structure pointer to display all student records.

C Program

#include <stdio.h>

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

int main()
{
    struct Student students[3] = {
        {101, "Rahul", 85.5},
        {102, "Aman", 91.0},
        {103, "Priya", 88.0}
    };

    struct Student *ptr = students;

    for (int i = 0; i < 3; i++)
    {
        printf("Student %d\n", i + 1);
        printf("Roll = %d\n", ptr->roll);
        printf("Name = %s\n", ptr->name);
        printf("Marks = %.2f\n\n", ptr->marks);

        ptr++;
    }

    return 0;
}

Sample Output

Student 1
Roll = 101
Name = Rahul
Marks = 85.50

Student 2
Roll = 102
Name = Aman
Marks = 91.00

Student 3
Roll = 103
Name = Priya
Marks = 88.00

Explanation

We create:

struct Student students[3];

Then:

struct Student *ptr = students;

The pointer points to the first structure in the array.

After processing the first student:

ptr++;

moves the pointer to the next struct Student.

So the pointer moves like:

ptr
 ↓
Student 1

ptr++
 ↓
Student 2

ptr++
 ↓
Student 3

This is an important combination of arrays, structures, and pointers.

Concepts Covered

  • Array of structures
  • Structure pointer
  • Pointer arithmetic
  • -> operator
  • for loop

Q10. Nested Structure with Pointer and Function

Problem Statement

Create a student structure containing a nested address structure. Pass the structure pointer to a function and display the complete student record.

C Program

#include <stdio.h>

struct Address
{
    char city[50];
    int pin;
};

struct Student
{
    int roll;
    char name[50];
    float marks;
    struct Address address;
};

void displayStudent(struct Student *student)
{
    printf("Roll = %d\n", student->roll);
    printf("Name = %s\n", student->name);
    printf("Marks = %.2f\n", student->marks);
    printf("City = %s\n", student->address.city);
    printf("PIN Code = %d\n", student->address.pin);
}

int main()
{
    struct Student student = {
        101,
        "Rahul",
        89.5,
        {"Delhi", 110075}
    };

    displayStudent(&student);

    return 0;
}

Sample Output

Roll = 101
Name = Rahul
Marks = 89.50
City = Delhi
PIN Code = 110075

Explanation

This program combines several concepts.

The Student structure contains:

struct Address address;

So it is a nested structure.

We create a pointer:

struct Student *student

inside the function.

The function receives the address:

displayStudent(&student);

Inside the function, normal members are accessed using:

student->roll
student->name
student->marks

The nested members are accessed using:

student->address.city
student->address.pin

This is an important pattern to understand before moving to more advanced structure and pointer programs.

Concepts Covered

  • Nested structures
  • Structure pointers
  • Functions
  • Array members
  • -> operator
  • Multiple levels of member access

Key Takeaways

  • A nested structure contains another structure as a member.
  • Nested structures are useful for representing related data in levels.
  • A structure pointer stores the address of a structure variable.
  • Use . with a normal structure variable.
  • Use -> with a pointer to a structure.
  • ptr->member is equivalent to (*ptr).member.
  • A structure pointer can modify the original structure.
  • Structure pointers can be passed to functions.
  • Arrays of structures can be processed using structure pointers.
  • Nested structures can also be accessed through structure pointers.
  • Structure pointers are an important foundation for linked lists and other data structures.
  • Always initialize a structure pointer before dereferencing it.

FAQs

1. What is a nested structure in C?

A nested structure is a structure that contains another structure as one of its members.

For example:

struct Student
{
    char name[50];
    struct Address address;
};

2. What is a structure pointer in C?

A structure pointer is a pointer that stores the address of a structure variable.

struct Student *ptr;

It can point to a struct Student object.

3. What is the difference between . and -> in C?

The dot operator is used with a normal structure variable:

student.name

The arrow operator is used with a structure pointer:

ptr->name

4. How do I access a nested structure using a normal structure variable?

Use multiple dot operators.

student.address.city

Here, address is a nested structure member.

5. How do I access a nested structure using a structure pointer?

Use -> for the outer structure pointer and . for the nested structure:

ptr->address.city

6. Can a structure pointer be passed to a function?

Yes. For example:

void display(struct Student *student)
{
    printf("%d", student->roll);
}

Call it with:

display(&student);

7. Why should I learn structure pointers in C?

Structure pointers are used extensively when modifying structures through functions, processing arrays of structures, working with dynamic memory, and building data structures such as linked lists.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top