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.member_name
- 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:
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student student1; // Declaring a structure variable
// Accessing and assigning values to the structure members
student1.age = 20; // Assigning a value to 'age'
student1.marks = 85.5; // Assigning a value to 'marks'
strcpy(student1.name, "Pawan Jaiswal"); // Assigning a value to 'name'
// Accessing and printing the structure members
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("Marks: %.2f\n", student1.marks);
return 0;
}
Output:
Name: Pawan Jaiswal
Age: 20
Marks: 85.50
Explanation:
- Dot Operator (
.
): This operator is used to access the members of the structure. For example,student1.age
gives you access to theage
member of thestudent1
structure. - Assigning Values: You can assign values to the members of the structure just like regular variables. For instance,
student1.age = 20;
assigns the value20
to theage
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
- Assigning Values to Members:
You can assign values to structure members individually using the dot operator:
student1.age = 20; // Assigning an integer value to 'age'
student1.marks = 85.5; // Assigning a float value to 'marks'
strcpy(student1.name, "Pawan Jaiswal"); // Assigning a string to 'name'
- Reading/Accessing Values from Members:
After assigning values to the members, you can access them using the dot operator:
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("Marks: %.2f\n", student1.marks);
Real-Life Example: Accessing Structure Members
Let’s create a structure to represent an Employee and access its members.
Example:
#include <stdio.h>
#include <string.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
struct Employee emp1; // Declaring an Employee structure variable
// Assigning values to the structure members
emp1.id = 101;
strcpy(emp1.name, "Pooja Jaiswal");
emp1.salary = 60000.50;
// Accessing and printing the structure members
printf("Employee ID: %d\n", emp1.id);
printf("Employee Name: %s\n", emp1.name);
printf("Employee Salary: %.2f\n", emp1.salary);
return 0;
}
Output:
Employee ID: 101
Employee Name: Pooja Jaiswal
Employee Salary: 60000.50
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:
#include <stdio.h>
#include <string.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
struct Employee employees[2]; // Array of structures for 2 employees
// Assigning values to employee 1
employees[0].id = 101;
strcpy(employees[0].name, "Pawan Jaiswal");
employees[0].salary = 55000.75;
// Assigning values to employee 2
employees[1].id = 102;
strcpy(employees[1].name, "Pooja Jaiswal");
employees[1].salary = 65000.80;
// Printing employee 1 details
printf("Employee 1 ID: %d\n", employees[0].id);
printf("Employee 1 Name: %s\n", employees[0].name);
printf("Employee 1 Salary: %.2f\n", employees[0].salary);
// Printing employee 2 details
printf("Employee 2 ID: %d\n", employees[1].id);
printf("Employee 2 Name: %s\n", employees[1].name);
printf("Employee 2 Salary: %.2f\n", employees[1].salary);
return 0;
}
Output:
Employee 1 ID: 101
Employee 1 Name: Pawan Jaiswal
Employee 1 Salary: 55000.75
Employee 2 ID: 102
Employee 2 Name: Pooja Jaiswal
Employee 2 Salary: 65000.80
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!