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.

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.

How the dot operator accesses structure members in C A structure variable named student contains three member compartments: name, age, and score. A dot operator selector combines student with a chosen member name. The examples student.age, student.score, and student.name connect to the matching compartment. A final panel shows student.age equals 15 changing only the age member. DOT OPERATOR SELECTS ONE MEMBER structure variable . member name MEMBER ACCESS PATTERN student . age STRUCTURE VARIABLE: student name "Ali" age 14 score 91.5 SELECT THE MEMBER YOU NEED NAME student.name AGE student.age SCORE student.score CHANGE ONE MEMBER student.age = 15; only the age member changes For a pointer to a structure, the arrow operator is commonly used; learn that separately.

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

Common mistakes when accessing structure members in C
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.
  1. If student is a structure variable, how do you access its score member?
  2. How do you change student.age to 16?
  3. If first and second are separate structure variables, does changing first.age automatically change second.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.