C Programming • Structure and Union
C Programming / Accessing elements of a structure

Accessing elements of a structure

Notes 4 Structure and Union

The individual members of a structure variable are accessed using the dot operator (.). The structure variable is written first, followed by a dot and the required member name.

Notes

The individual members of a structure variable are accessed using the dot operator (.). The structure variable is written first, followed by a dot and the required member name.

💡 Example
student.rollNo student.percentage

Here, student is the structure variable and rollNo and percentage are its members.

Syntax

structure_variable.member_name
💡 Example

s1.rollNo accesses the rollNo member of the structure variable s1.

Reading a Structure Member

A structure member can be used to read its stored value just like an ordinary variable.

💡 Example
printf("%d", s1.rollNo);

The value stored in rollNo of s1 is displayed.

Updating a Structure Member

A structure member can also be assigned a new value using the dot operator.

💡 Example
s1.rollNo = 105;

The rollNo member of s1 is changed to 105.

Accessing Different Members

Each member is accessed separately using the same structure variable followed by the dot operator.

💡 Example
s1.rollNo s1.name s1.percentage

Each statement accesses a different member of the same structure variable.

Practical Example

Problem Statement

Write a C program to define a structure for a student, accept the student's roll number and percentage, and then display these values by accessing the structure members.

Learning Outcomes

  • Use the dot operator to access structure members.
  • Read values into individual structure members.
  • Display stored values using a structure variable.

Hint

Create a structure named Student and use s1.rollNo and s1.percentage to access its members.

Theory

The dot operator is used with a structure variable to access its individual members. Each member can be read, assigned, or passed to a function separately.

Program

#include <stdio.h> struct Student { int rollNo; float percentage; }; int main() { struct Student s1; printf("Enter roll number: "); scanf("%d", &s1.rollNo); printf("Enter percentage: "); scanf("%f", &s1.percentage); // access and display individual structure members printf("Roll Number = %d ", s1.rollNo); printf("Percentage = %.1f ", s1.percentage); return 0; }

Expected Output

Enter roll number: 101

Enter percentage: 82.5

Roll Number = 101

Percentage = 82.5

Note

The dot operator is used when a normal structure variable is used to access its members. Pointer-based structure access using -> is a separate concept and is covered with pointers.

Common Mistake

Students often forget to write the structure variable before the dot operator. The member name alone is not enough to identify which structure record should be accessed.

⚠️ Example

Incorrect: rollNo

Correct: s1.rollNo

Quick Revision

Concept Remember
Dot Operator Used to access members of a structure variable.
Syntax structure_variable.member_name
Read Member Use the member expression to obtain its value.
Update Member Assign a new value using the member expression.

Important Exam Questions

Short Answer Questions

  1. Which operator is used to access a structure member?
  2. Write the syntax for accessing a structure member.
  3. How do you access the roll number of a student structure variable?
  4. How can a structure member be updated?
  5. What is the difference between a structure variable and a structure member?

Long Answer Questions

  1. Explain how structure members are accessed using the dot operator with suitable examples.
  2. Write a C program to accept and display student information using structure members.
  3. Explain how to read and modify individual members of a structure.
🎥 Recommended Learning

Watch a beginner-friendly explanation of accessing structure members in C.

▶ Watch: Accessing Structure Members in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for accessing structure members will be provided here.

🧠 Mind Map

Use the mind map for quick revision of structure variable, member and dot operator.