Intermediate C taught explicit ownership, dynamic arrays, parsing, callbacks and failure tests. Advanced C combines those tools into one reusable library: a decoder for binary telemetry that arrives in pieces. No network or C++ knowledge is required. Over seven days you will build it the way real C libraries are built, with every pointer, byte and failure path covered by a written contract and a test.
Start with a problem that every C library meets. Suppose the header publishes the decoder's struct:
typedef struct {
unsigned char frame[22];
size_t used;
int failed;
} Decoder;Now any caller can write decoder.used = 3; or decoder.failed = 0; and skip the validation that the library depends on. Worse, the layout itself becomes part of the public interface. If version 2 of the library adds a field, sizeof(Decoder) changes, and every program that was compiled against version 1 now allocates the wrong amount of storage. Callers did not touch a line of code, yet their binaries are broken.
An opaque type solves both problems. The header declares typedef struct Decoder Decoder; and nothing more. Callers can hold a Decoder *, pass it around and compare it with NULL, but they cannot see a field or compute the size. The implementation file completes the struct, so only the library's own functions can touch the representation.
Predict and run
Read the program before pressing Run. Write down its output and say which line produces it.
#include <stdio.h>
typedef struct Counter Counter;
int counter_value(const Counter *counter);
struct Counter { int value; };
int counter_value(const Counter *counter) { return counter->value; }
int main(void) {
Counter counter = {3};
printf("%d\n", counter_value(&counter));
return 0;
}Check your prediction
3The first two lines are what a public header would contain: an incomplete type and a function that accepts a pointer to it. The struct Counter definition completes the type, and only after that point can main create a Counter by value. This one-file version is for tracing. In a real library, main would live in another file that never sees the definition.
What an incomplete type allows
Until the compiler sees the closing brace of struct Counter, the type is incomplete. The compiler knows a struct with that tag exists, but not how big it is. That limits what code can do with it:
- Allowed: declare pointers, pass them to functions, return them, store them in other structs, and compare them with
NULLor with each other. - Not allowed: declare an object of the type, use
sizeof, access a member, copy it by assignment, or do pointer arithmetic such ascounter + 1. All of those need the size or the layout.
Move the definition below main and the compiler rejects the object declaration:
#include <stdio.h>
typedef struct Counter Counter;
int counter_value(const Counter *counter);
int main(void) {
Counter counter = {3};
printf("%d\n", counter_value(&counter));
return 0;
}main.c:7:13: error: variable has incomplete type 'Counter' (aka 'struct Counter')That error is exactly the protection you want. A caller who only has the header cannot build a Counter on the stack, so the library decides how and where every Counter is created. The consequence is that the library must also provide a function that creates one, which is the subject of the next lesson.
Split the header from the implementation
A typical layout has three parts. The public header holds only the incomplete type and the operations:
/* counter.h */
typedef struct Counter Counter;
Counter *counter_create(void);
void counter_add(Counter *counter, int amount);
int counter_value(const Counter *counter);
void counter_destroy(Counter *counter);The implementation file includes the header and then completes the type. It is the only place where struct Counter { int value; }; appears. Callers include counter.h and link against the compiled implementation, so the library can add, remove or reorder fields without recompiling a single caller.
The program below simulates that split in one file. Everything above the caller comment is the library; main uses only the four functions, exactly as a separate file would have to.
#include <stdio.h>
#include <stdlib.h>
/* Public part: what counter.h would declare. */
typedef struct Counter Counter;
Counter *counter_create(void);
void counter_add(Counter *counter, int amount);
int counter_value(const Counter *counter);
void counter_destroy(Counter *counter);
/* Private part: what counter.c would contain. */
struct Counter { int value; };
Counter *counter_create(void) {
Counter *counter = malloc(sizeof *counter);
if (counter != NULL) { counter->value = 0; }
return counter;
}
void counter_add(Counter *counter, int amount) { counter->value += amount; }
int counter_value(const Counter *counter) { return counter->value; }
void counter_destroy(Counter *counter) { free(counter); }
/* Caller: uses only the declared operations. */
int main(void) {
Counter *counter = counter_create();
if (counter == NULL) { return 1; }
counter_add(counter, 4);
counter_add(counter, 3);
printf("Value: %d\n", counter_value(counter));
counter_destroy(counter);
return 0;
}Value: 7Notice the const Counter * parameter on counter_value. A getter that promises not to modify the object tells callers they can call it on a handle they only have read access to, and the compiler enforces that promise inside the library.
What opaque types do not protect
An opaque type stops callers from reaching into fields. It does not make C memory-safe. A caller can still pass NULL, a pointer that was already destroyed, or a pointer to some unrelated object cast to Counter *. Each of those is undefined behavior: the C standard places no requirement on what happens, so the program may crash, may appear to work, or may silently corrupt other data. The library cannot detect most of these mistakes, because a dangling pointer looks exactly like a valid one.
That is why every function in the project's header carries a written precondition, such as "requires non-NULL decoder". The opaque type removes one class of mistakes, and the contract describes the rest. You will see the same split all week: the compiler enforces what it can, and documentation plus tests cover the remainder.
Practice
Your turn
Build an opaque Meter with a fixed limit. Write meter_create(limit), meter_add(meter, amount), meter_value(meter) and meter_destroy(meter). meter_add returns 1 and adds the amount when the new total would stay within the limit; otherwise it returns 0 and leaves the value unchanged. Check the limit without computing value + amount, so the test cannot overflow. In main, use only the four functions: create a meter with limit 100, add 10, 20 and 80, and print:
Added 10: ok
Added 20: ok
Added 80: rejected
Value: 30#include <stdio.h>
#include <stdlib.h>
typedef struct Meter Meter;
Meter *meter_create(unsigned limit);
int meter_add(Meter *meter, unsigned amount);
unsigned meter_value(const Meter *meter);
void meter_destroy(Meter *meter);
/* Complete struct Meter and the four functions here. */
int main(void) {
/* Use only the declared operations. */
return 0;
}Hint 1: the idea
The struct needs two fields: the current value and the limit. Callers never see either one, so you can store whatever makes the check easy.
Hint 2: a first step
Write the struct with its two fields and the create function first, then print how much room a fresh meter has left. Write the add check in terms of that room rather than value + amount, which is the expression that can wrap.
Show one solution
#include <stdio.h>
#include <stdlib.h>
typedef struct Meter Meter;
Meter *meter_create(unsigned limit);
int meter_add(Meter *meter, unsigned amount);
unsigned meter_value(const Meter *meter);
void meter_destroy(Meter *meter);
struct Meter { unsigned value; unsigned limit; };
Meter *meter_create(unsigned limit) {
Meter *meter = malloc(sizeof *meter);
if (meter != NULL) { meter->value = 0; meter->limit = limit; }
return meter;
}
int meter_add(Meter *meter, unsigned amount) {
if (amount > meter->limit - meter->value) { return 0; }
meter->value += amount;
return 1;
}
unsigned meter_value(const Meter *meter) { return meter->value; }
void meter_destroy(Meter *meter) { free(meter); }
int main(void) {
Meter *meter = meter_create(100);
if (meter == NULL) { return 1; }
const unsigned amounts[3] = {10, 20, 80};
for (size_t i = 0; i < 3; ++i) {
printf("Added %u: %s\n", amounts[i], meter_add(meter, amounts[i]) ? "ok" : "rejected");
}
printf("Value: %u\n", meter_value(meter));
meter_destroy(meter);
return 0;
}Added 10: ok
Added 20: ok
Added 80: rejected
Value: 30The invariant value <= limit holds after every call, and only meter_add can change value, so no caller can break it. That guarantee is the real payoff of hiding the struct. The subtraction form of the check works because the invariant keeps limit - value from wrapping; you will reuse the same trick for buffer bounds on Day 2. Next, you make the create and destroy pair a precise contract, including what happens when allocation fails.
Completion is saved in this browser only. It does not measure mastery.