C Programming • Structure and Union
C Programming / Accessing Elements of a Structure — Exercises

Accessing Elements of a Structure — Exercises

Practical 4 Structure and Union

Write a C program to create a Student structure and access its roll number, name and marks using the dot operator.

Practical / Solution

Accessing Structure Elements — Exercise 1

Problem

Write a C program to create a Student structure and access its roll number, name and marks using the dot operator.

Program

#include <stdio.h> struct Student { int roll; char name[50]; float marks; }; int main() { struct Student s; printf("Enter roll number: "); scanf("%d", &s.roll); printf("Enter name: "); scanf("%49s", s.name); printf("Enter marks: "); scanf("%f", &s.marks); // Access structure members using dot operator printf("\nRoll Number = %d\n", s.roll); printf("Name = %s\n", s.name); printf("Marks = %.2f\n", s.marks); return 0; }

Explanation

The dot operator . is used to access a member of a structure variable. For example, s.roll accesses the roll member of structure variable s.

Expected Output

Enter roll number: 101

Enter name: Rahul

Enter marks: 85

Roll Number = 101

Name = Rahul

Marks = 85.00

Accessing Structure Elements — Exercise 2

Problem

Write a C program to initialize a structure variable and access individual members using the dot operator.

Program

#include <stdio.h> struct Book { int id; char title[50]; float price; }; int main() { // Initialize structure variable struct Book book = {101, "Programming", 500.0}; // Access members individually printf("Book ID = %d\n", book.id); printf("Title = %s\n", book.title); printf("Price = %.2f\n", book.price); return 0; }

Explanation

Each member of a structure can be accessed independently using the structure variable followed by the dot operator.

Expected Output

Book ID = 101

Title = Programming

Price = 500.00

Accessing Structure Elements — Exercise 3

Problem

Write a C program to modify the marks of a student after the structure has been initialized and display the updated value.

Program

#include <stdio.h> struct Student { int roll; char name[50]; float marks; }; int main() { struct Student s = {101, "Amit", 70.0}; printf("Original marks = %.2f\n", s.marks); // Modify a structure member s.marks = 82.5; printf("Updated marks = %.2f\n", s.marks); return 0; }

Explanation

Structure members are not read-only. A member can be modified directly using the dot operator and assignment operator.

Expected Output

Original marks = 70.00

Updated marks = 82.50

Accessing Structure Elements — Exercise 4

Problem

Write a C program to create two employee structure variables and access the members of both variables separately.

Program

#include <stdio.h> struct Employee { int id; char name[50]; float salary; }; int main() { struct Employee e1, e2; printf("Enter first employee ID, name and salary: "); scanf("%d %49s %f", &e1.id, e1.name, &e1.salary); printf("Enter second employee ID, name and salary: "); scanf("%d %49s %f", &e2.id, e2.name, &e2.salary); // Access members of first employee printf("\nEmployee 1:\n"); printf("ID = %d\n", e1.id); printf("Name = %s\n", e1.name); printf("Salary = %.2f\n", e1.salary); // Access members of second employee printf("\nEmployee 2:\n"); printf("ID = %d\n", e2.id); printf("Name = %s\n", e2.name); printf("Salary = %.2f\n", e2.salary); return 0; }

Explanation

Each structure variable has its own set of members. The same member name can be accessed independently through different structure variables.

Expected Output

Enter first employee ID, name and salary: 101 Amit 40000

Enter second employee ID, name and salary: 102 Neha 45000

Employee 1:

ID = 101

Name = Amit

Salary = 40000.00

Employee 2:

ID = 102

Name = Neha

Salary = 45000.00

Accessing Structure Elements — Exercise 5

Problem

Write a C program to calculate the total and average marks by accessing subject marks stored as structure members.

Program

#include <stdio.h> struct Student { char name[50]; int math; int cProgramming; int computer; }; int main() { struct Student s; int total; float average; printf("Enter name: "); scanf("%49s", s.name); printf("Enter marks in Mathematics, C Programming and Computer: "); scanf("%d %d %d", &s.math, &s.cProgramming, &s.computer); // Access and process individual members total = s.math + s.cProgramming + s.computer; average = total / 3.0; printf("\nStudent = %s\n", s.name); printf("Total = %d\n", total); printf("Average = %.2f\n", average); return 0; }

Explanation

Each subject is represented as a separate structure member. The dot operator is used to access those members in an expression.

Expected Output

Enter name: Priya

Enter marks in Mathematics, C Programming and Computer: 80 90 85

Student = Priya

Total = 255

Average = 85.00

Accessing Structure Elements — Exercise 6

Problem

Write a C program to access structure members through a pointer using the arrow operator ->.

Program

#include <stdio.h> struct Student { int roll; char name[50]; float marks; }; int main() { struct Student s; struct Student *ptr; ptr = &s; printf("Enter roll number: "); scanf("%d", &ptr->roll); printf("Enter name: "); scanf("%49s", ptr->name); printf("Enter marks: "); scanf("%f", &ptr->marks); // Access structure members using pointer printf("\nRoll = %d\n", ptr->roll); printf("Name = %s\n", ptr->name); printf("Marks = %.2f\n", ptr->marks); return 0; }

Explanation

When a pointer points to a structure, its members can be accessed conveniently using the arrow operator ->. For example, ptr->roll accesses the roll member.

Expected Output

Enter roll number: 101

Enter name: Rahul

Enter marks: 88

Roll = 101

Name = Rahul

Marks = 88.00

Accessing Structure Elements — Exercise 7

Problem

Write a C program to access a structure through a pointer and modify one of its members.

Program

#include <stdio.h> struct Employee { int id; float salary; }; int main() { struct Employee emp = {101, 40000.0}; struct Employee *ptr = &emp; printf("Original salary = %.2f\n", ptr->salary); // Modify the original member through pointer ptr->salary = 50000.0; printf("Updated salary = %.2f\n", ptr->salary); return 0; }

Explanation

The pointer stores the address of the structure variable. Using ->, the program can directly read or modify the structure members through that pointer.

Expected Output

Original salary = 40000.00

Updated salary = 50000.00

Accessing Structure Elements — Exercise 8

Problem

Write a C program to access the members of a structure passed to a function using a structure pointer.

Program

#include <stdio.h> struct Product { int id; char name[50]; float price; }; void displayProduct(struct Product *p) { // Access structure members through pointer printf("Product ID = %d\n", p->id); printf("Product Name = %s\n", p->name); printf("Price = %.2f\n", p->price); } int main() { struct Product p; printf("Enter product ID: "); scanf("%d", &p.id); printf("Enter product name: "); scanf("%49s", p.name); printf("Enter price: "); scanf("%f", &p.price); // Pass structure address to function displayProduct(&p); return 0; }

Explanation

Passing the structure address allows the function to access its members through a pointer. The arrow operator is used inside the function.

Expected Output

Enter product ID: 201

Enter product name: Keyboard

Enter price: 800

Product ID = 201

Product Name = Keyboard

Price = 800.00

Accessing Structure Elements — Exercise 9

Problem

Write a C program to calculate the total salary of two employees by accessing their salary members.

Program

#include <stdio.h> struct Employee { int id; char name[50]; float salary; }; int main() { struct Employee e1, e2; float totalSalary; printf("Enter first employee ID, name and salary: "); scanf("%d %49s %f", &e1.id, e1.name, &e1.salary); printf("Enter second employee ID, name and salary: "); scanf("%d %49s %f", &e2.id, e2.name, &e2.salary); // Access salary members and calculate total totalSalary = e1.salary + e2.salary; printf("Total Salary = %.2f\n", totalSalary); return 0; }

Explanation

Structure members can be used directly in arithmetic expressions. Here, the salary member of both structure variables is accessed separately and added.

Expected Output

Enter first employee ID, name and salary: 101 Amit 40000

Enter second employee ID, name and salary: 102 Neha 45000

Total Salary = 85000.00

Accessing Structure Elements — Exercise 10

Problem

Write a C program to create a student structure, access its members using both the dot operator and a structure pointer, and display the complete record.

Program

#include <stdio.h> struct Student { int roll; char name[50]; float marks; }; int main() { struct Student s; struct Student *ptr; ptr = &s; printf("Enter roll number: "); scanf("%d", &s.roll); printf("Enter name: "); scanf("%49s", s.name); printf("Enter marks: "); scanf("%f", &s.marks); // Access using dot operator printf("\nUsing dot operator:\n"); printf("Roll = %d\n", s.roll); printf("Name = %s\n", s.name); printf("Marks = %.2f\n", s.marks); // Access the same members through a pointer printf("\nUsing arrow operator:\n"); printf("Roll = %d\n", ptr->roll); printf("Name = %s\n", ptr->name); printf("Marks = %.2f\n", ptr->marks); return 0; }

Explanation

A normal structure variable accesses members with ., while a pointer to a structure accesses the same members with ->. Both refer to the same underlying structure data.

Expected Output

Enter roll number: 101

Enter name: Rahul

Enter marks: 87

Using dot operator:

Roll = 101

Name = Rahul

Marks = 87.00

Using arrow operator:

Roll = 101

Name = Rahul

Marks = 87.00