C Access Structure Members
After creating a structure variable, you often need to read or change one of its members.
For a normal structure object, C uses the dot operator . to select a member. Review Structures and Declare Structure first if needed.
Quick Answer
Use structure_variable.member to access a member of a normal structure object. For example, student.age reads or refers to the age member of student, and student.age = 15; changes that member.
What Does Accessing a Member Mean?
A structure can hold several related values.
Member access means selecting one specific value inside one structure variable.
student.age
student.score
These expressions select different members from the same structure object.
C Structure Member Access Syntax
structure_variable.member_name
Example:
student.age
The part before the dot is the structure variable. The part after the dot is the member name.
C Access Structure Members Example
This program reads two members and then changes one member with the dot operator.
#include <stdio.h>
struct Student {
int age;
float score;
};
int main(void) {
struct Student student = {14, 91.5f};
printf("Age: %d\n", student.age);
printf("Score: %.1f\n", student.score);
student.age = 15;
printf("New age: %d\n", student.age);
return 0;
}
Output
Age: 14
Score: 91.5
New age: 15
student.age selects the age member. The assignment changes only that member of student.
Read a Structure Member
You can use a member in an expression just like another value of its type:
printf("%d\n", student.age);
Here, student.age produces the value stored in the age member.
Change a Structure Member
If the structure object is modifiable, you can assign a new value to one of its members:
student.age = 15;
This changes age but leaves the other members unchanged.
Each Member Keeps Its Own Data Type
Suppose the structure is:
struct Student {
int age;
float score;
};
Then student.age has type int, while student.score has type float.
Use each member according to its own type.
The Same Member Name Can Exist in Different Structure Variables
If you have two variables:
struct Student first = {14, 91.5f};
struct Student second = {16, 87.0f};
then first.age and second.age are different member objects.
Changing one does not automatically change the other.
Use Dot with a Normal Structure Object
For a normal structure variable, use:
student.age
A pointer to a structure is accessed differently and commonly uses ->. That is a separate pointer-to-structure topic.
Common Member Access Mistakes
| Mistake | Correct Idea |
|---|---|
Writing only age when the value belongs to student. |
Select it with student.age. |
| Using a member name that is not declared in the structure type. | Use one of the members actually listed in the structure definition. |
| Using the wrong format specifier for a member. | Match the member's type, such as %d for an int. |
Using -> with a normal structure object. |
Use . for a normal structure object. |
- If
studentis a structure variable, how do you access itsscoremember? - How do you change
student.ageto16? - If
firstandsecondare separate structure variables, does changingfirst.ageautomatically changesecond.age?
Practice Answers
1. How do you access student score?
student.score
The dot operator selects the score member.
2. How do you change student age to 16?
student.age = 16;
3. Does changing first.age change second.age?
No. They are members of two different structure variables.
FAQs
How do you access a structure member in C?
Use the dot operator between the structure variable name and the member name, such as student.age.
Can you change a structure member after creating the variable?
Yes. For a modifiable structure object, assign a new value to the member, such as student.age = 15;.
What does student.score mean in C?
It selects the score member that belongs to the structure variable named student.
Do you use the dot operator with a pointer to a structure?
Usually no. The dot operator is used with a structure object. A pointer to a structure commonly uses the arrow operator, which is a separate topic.
Summary
Use the dot operator to access members of a normal C structure object. Write structure_variable.member_name, such as student.age. You can read a member, use it in expressions, or assign a new value to it when the structure object is modifiable. Each structure variable has its own member values.