Helper Methods
Part of the Object Oriented Programming section of Coddy's C journey — lesson 10 of 61.
So far, we've seen functions that modify struct data (like player_take_damage) and functions that simply display it. But there's another important category: functions that compute derived values from struct members without changing anything.
Consider a Rectangle struct that stores width and height. The area isn't stored—it's calculated on demand:
typedef struct {
double width;
double height;
} Rectangle;
double rectangle_area(const Rectangle *self) {
return self->width * self->height;
}
double rectangle_perimeter(const Rectangle *self) {
return 2 * (self->width + self->height);
}
These are helper methods—they take the struct's existing data and produce a new value. Notice they use const pointers since they only read, never write. The computed result is returned rather than stored in the struct.
Rectangle box = {5.0, 3.0};
printf("Area: %.1f\n", rectangle_area(&box)); // 15.0
printf("Perimeter: %.1f\n", rectangle_perimeter(&box)); // 16.0
This pattern keeps your structs lean—store only the essential data, and compute everything else when needed. It also ensures derived values are always consistent with the current state.
Challenge
EasyLet's build a Circle module that calculates derived values from stored data. You'll practice writing helper methods—functions that compute results from struct members without modifying them.
You'll create three files:
circle.h: Declare aCirclestruct with a singledouble radiusmember. Also declare two helper methods:circle_area— takes aconstpointer to Circle and returns the areacircle_circumference— takes aconstpointer to Circle and returns the circumference
CIRCLE_H.circle.c: Implement both helper methods. For the calculations, use3.14159as the value of pi. The area formula ispi * radius * radius, and the circumference formula is2 * pi * radius. Both functions should only read the radius—never modify it.main.c: Create a Circle and use the helper methods to compute and display its properties.
You will receive one input: the radius of the circle (as a double).
In your main file, initialize a Circle with the given radius, then use your helper methods to calculate and print the area and circumference.
Print the results in this format:
Area: {area}
Circumference: {circumference}Use %.2f for both values to display two decimal places.
For example, with a radius of 5.0, the output would be:
Area: 78.54
Circumference: 31.42Cheat sheet
Functions can compute derived values from struct members without modifying the struct. These are called helper methods.
Helper methods use const pointers since they only read data, never write:
typedef struct {
double width;
double height;
} Rectangle;
double rectangle_area(const Rectangle *self) {
return self->width * self->height;
}
double rectangle_perimeter(const Rectangle *self) {
return 2 * (self->width + self->height);
}
Using helper methods:
Rectangle box = {5.0, 3.0};
printf("Area: %.1f\n", rectangle_area(&box)); // 15.0
printf("Perimeter: %.1f\n", rectangle_perimeter(&box)); // 16.0
This pattern keeps structs lean by storing only essential data and computing derived values on demand, ensuring consistency with the current state.
Try it yourself
#include <stdio.h>
#include "circle.h"
int main() {
double radius;
scanf("%lf", &radius);
// TODO: Create a Circle struct with the given radius
// TODO: Use circle_area and circle_circumference helper methods
// to calculate and print the results
// Print format:
// printf("Area: %.2f\n", ...);
// printf("Circumference: %.2f\n", ...);
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