Menu
Coddy logo textTech

Accessing Parent Members

Part of the Object Oriented Programming section of Coddy's C journey — lesson 30 of 61.

Now that we understand struct embedding and the first member rule, let's focus on the practical side: how to read and modify the embedded parent's data from within a function.

When a function receives a Child struct (or a pointer to one), accessing the parent's members requires navigating through the embedded struct. The syntax depends on whether you're working with a value or a pointer.

typedef struct {
    int id;
    int score;
} Parent;

typedef struct {
    Parent parent;
    char grade;
} Child;

With a pointer to Child, use the arrow operator to reach the parent, then dot to access its members:

void update_score(Child* c, int new_score) {
    c->parent.score = new_score;  // Arrow then dot
}

void print_info(Child* c) {
    printf("ID: %d, Score: %d\n", c->parent.id, c->parent.score);
}

With a value (passed by copy), use dots throughout:

void show_id(Child c) {
    printf("ID: %d\n", c.parent.id);  // Dot then dot
}

This chained access pattern — child->parent.member or child.parent.member — is how you interact with inherited data in C's composition model. It's slightly more verbose than true inheritance, but it's explicit and clear about the relationship between types.

challenge icon

Challenge

Easy

Let's build a student grading system that demonstrates how to access and modify data inside an embedded struct. You'll create a hierarchy where a Student contains an embedded Person, then write functions that work with the parent's data through the child.

You'll organize your code across three files:

  • student.h: Define your struct hierarchy with include guards. Create a Person struct with name (a character array of 50 characters) and age (an integer). Then define a Student struct that embeds Person as its first member and adds a grade field (an integer representing their score). Declare two functions: set_person_age that takes a Student* and a new age value, and print_student that takes a Student* and displays all information.
  • student.c: Implement both functions. Your set_person_age function should modify the age inside the embedded Person struct—this is where you'll practice the arrow-then-dot syntax (s->person.age). Your print_student function should access and display all fields from both the embedded person and the student itself.
  • main.c: Create a Student variable and initialize all its fields. Then use set_person_age to update the embedded person's age, and call print_student to display the result.

You will receive four inputs: the student's name (a string), their initial age (an integer), their grade (an integer), and a new age (an integer to update via your function).

In your main file, create the student with the initial values, then call set_person_age to change the age to the new value, and finally call print_student to display the updated information.

Your output should look like this:

Name: Emma
Age: 21
Grade: 85

Where Emma is the name, 21 is the updated age (not the initial age), and 85 is the grade. The key learning here is navigating through the embedded struct to both read and write the parent's members.

Cheat sheet

To access embedded parent struct members from a function, use chained access syntax that depends on whether you're working with a pointer or a value.

With a pointer to the child struct, use the arrow operator to reach the embedded parent, then dot notation to access its members:

void update_score(Child* c, int new_score) {
    c->parent.score = new_score;  // Arrow then dot
}

void print_info(Child* c) {
    printf("ID: %d, Score: %d\n", c->parent.id, c->parent.score);
}

With a value (passed by copy), use dot notation throughout:

void show_id(Child c) {
    printf("ID: %d\n", c.parent.id);  // Dot then dot
}

This pattern — child->parent.member or child.parent.member — provides explicit access to inherited data in C's composition model.

Try it yourself

#include <stdio.h>
#include <string.h>
#include "student.h"

int main() {
    char name[50];
    int initial_age, grade, new_age;
    
    // Read inputs
    fgets(name, 50, stdin);
    name[strcspn(name, "\n")] = '\0';  // Remove newline
    scanf("%d", &initial_age);
    scanf("%d", &grade);
    scanf("%d", &new_age);
    
    // TODO: Create a Student variable
    
    // TODO: Initialize all fields of the student
    // - Copy name to the embedded person's name using strcpy
    // - Set the embedded person's age to initial_age
    // - Set the student's grade
    
    // TODO: Call set_person_age to update the age to new_age
    
    // TODO: Call print_student to display the result
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming