C Programming·The fundamentals of memory

Structs and function design

Structs group related data; functions should state who owns what.

Beginner 14 min read Lesson 8 of 300 Challenge included
What you will be able to do
  • Use struct for a fixed group of related fields.
  • Return an explicit error enum for recoverable failures.
  • Document ownership: who allocates and who frees.

In plain English

Pass large structs by pointer to avoid copies, and mark read-only pointers const. A function returns an error code or a sentinel; it should not print and continue as if nothing happened. Keep allocation and freeing close to the same owner.

course.cC
#include <stdio.h>

typedef struct {
    const char *slug;
    const char *title;
    int minutes;
} Course;

void print_course(const Course *course) {
    printf("%s (%s) · %d min\n",
           course->title, course->slug, course->minutes);
}

int main(void) {
    Course html = { "html", "HTML Foundations", 120 };
    print_course(&html);
    return 0;
}
Your turn

Add a course printer

Medium

Define a Course struct and a function that prints title and minutes.

C · starter
typedef struct {
    const char *title;
    int minutes;
} Course;
Check your understanding before moving on.
Knowledge checkQuestion 1

Why pass a large struct as a const pointer?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Dynamic memory
Lesson 8 of 300 · 0% of course