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
EasyLet'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 aPersonstruct withname(a character array of 50 characters) andage(an integer). Then define aStudentstruct that embedsPersonas its first member and adds agradefield (an integer representing their score). Declare two functions:set_person_agethat takes aStudent*and a new age value, andprint_studentthat takes aStudent*and displays all information.student.c: Implement both functions. Yourset_person_agefunction should modify the age inside the embeddedPersonstruct—this is where you'll practice the arrow-then-dot syntax (s->person.age). Yourprint_studentfunction should access and display all fields from both the embedded person and the student itself.main.c: Create aStudentvariable and initialize all its fields. Then useset_person_ageto update the embedded person's age, and callprint_studentto 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: 85Where 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;
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Object Oriented Programming
1Modular Programming Basics
Header FilesInclude GuardsSource FilesStatic FunctionsRecap: Modular Calculator4Encapsulation
Opaque Pointers ConceptDefining Opaque StructsGetters and SettersValidation in SettersRecap: Secret Box2Objects and Methods
Structs as ObjectsThe 'Self' PointerConst CorrectnessPointer vs ValueHelper MethodsRecap: Point Manager5Project: Simple Bank Account
Project SetupImplementation of Account3Object Lifecycle
Constructor PatternDestructor PatternStack InitializationDeep CopyRecap: String Wrapper6Inheritance via Composition
Struct EmbeddingThe First Member RuleAccessing Parent MembersUpcastingRecap: Shape Hierarchy