Accessing Structure Members in C Programming

Once you have defined a structure in C, the next important step is to access and manipulate its members. The members of a structure are the variables that are defined inside it. To work with these members, you need to use the dot operator (.) to access individual elements.

At SamagraCS Educational Technology, we make sure you understand the concept of accessing structure members in a simple, clear, and relatable way.


How to Access Structure Members

After you declare a structure variable, you can access its members using the following syntax:

  • structure_variable: This is the name of the variable that holds the structure.
  • member_name: This is the name of the member (variable) you want to access inside the structure.

Example:

Output:


Explanation:

  • Dot Operator (.): This operator is used to access the members of the structure. For example, student1.age gives you access to the age member of the student1 structure.
  • Assigning Values: You can assign values to the members of the structure just like regular variables. For instance, student1.age = 20; assigns the value 20 to the age member.
  • Accessing Values: To access and print the values stored in the structure members, you use the dot operator with printf().

Structure Member Access in Detail

  1. Assigning Values to Members:

You can assign values to structure members individually using the dot operator:

  1. Reading/Accessing Values from Members:

After assigning values to the members, you can access them using the dot operator:


Real-Life Example: Accessing Structure Members

Let’s create a structure to represent an Employee and access its members.

Example:

Output:

Explanation:

  • The structure Employee holds the employee’s ID, name, and salary.
  • We access these members using the dot operator (e.g., emp1.id).
  • We can then print the values of these members using printf().

Using Structure Arrays

You can also create arrays of structures and access individual members in a similar way. Here’s an example with multiple employees:

Example:

Output:


Accessing structure members in C is straightforward using the dot operator. Structures help in grouping different types of data under one umbrella, and by using the dot operator, you can easily assign values to or retrieve values from the members of the structure.

At SamagraCS Educational Technology, we believe in making concepts simple and easy to understand. Practice accessing structure members through real-life examples to get a better grasp of this concept.

For any questions, feel free to reach out to Pawan &Jaiswal, or the team at SamagraCS Educational Technology!

error: Content is protected !!
Open chat
1
Hi,how Can We Help You ?