To access members of a structure in C, you use the dot (.) operator. The dot operator allows you to access and manipulate individual members of a structure variable.
Here's the syntax:
structure_variable.member_name
Let's say you have a structure named Person with members name, age, and height. To access these members, you would use the dot operator as follows:
struct Person person1;
person1.age = 25; // Accessing and assigning a value to the 'age' member
strcpy(person1.name, "John Doe"); // Accessing and assigning a value to the 'name' member
person1.height = 1.75; // Accessing and assigning a value to the 'height' member
In this example, we create a structure variable person1 of type Person. To assign a value to the age member, we use person1.age with the dot operator. Similarly, we use person1.name to assign a value to the name member, and person1.height to assign a value to the height member.
You can also read the values of structure members using the same dot operator.
For example:
printf("Name: %s\n", person1.name); // Accessing and printing the value of the 'name' member
printf("Age: %d\n", person1.age); // Accessing and printing the value of the 'age' member
printf("Height: %.2f\n", person1.height); // Accessing and printing the value of the 'height' member
In this case, we use person1.name, person1.age, and person1.height to access and print the values of the respective members.
Remember to use the dot operator (.) to access the specific member of a structure variable you want to read or modify.