Opaque Pointers
An opaque pointer is a pointer to a struct the caller cannot see the members of. The header declares typedef struct HashTable HashTable; and functions that take HashTable *. The layout lives in one .c file.
Detailed Explanation
Data structures in C leak their representation the moment the struct is in the header. Callers then write t->buckets[i] and you can never change the field name. An opaque pointer puts a compiler wall in front of that. The type is incomplete in the header. sizeof(HashTable) is an error. HashTable t; is an error. HashTable *t is fine.
This is the same idea as an opaque type in the OOP sense, applied to containers. FILE * is the standard library's opaque pointer. DIR * is POSIX's. Your Graph *, Heap *, and Map * should look the same if you intend to ship a library.
The implementation file defines struct HashTable { ... }; and includes the public header. Functions there can touch members. They return HashTable * from hashtable_new and take it everywhere else. The caller stores the pointer, passes it, and never dereferences it except through the API.
Because the size is hidden, the object is heap-allocated (or allocated from a pool the library owns). The caller cannot embed a HashTable by value inside another struct. They embed a HashTable *, which is a nullable handle. NULL means "no table" or "allocation failed." Document which, and do not dereference.
A lighter opacity is a pointer to void with no typedef. That compiles and throws away type checking: any void * can be passed. Prefer an incomplete struct tag. The compiler will then reject hashtable_free(graph).
Changing the layout does not require a header change, which means existing object files that only called functions still work. Changing the functions still requires a rebuild. Opacity protects representation, not ABI of the function list.
Code Examples
/* map.h */
#ifndef MAP_H
#define MAP_H
typedef struct Map Map;
Map *map_new(void);
void map_free(Map *m);
int map_put(Map *m, int key, int value);
int map_get(const Map *m, int key, int *out);
#endif/* map.c */
#include "map.h"
#include <stdlib.h>
enum { CAP = 4 };
struct Map {
int keys[CAP];
int vals[CAP];
int len;
};
Map *map_new(void)
{
Map *m = calloc(1, sizeof *m);
return m;
}
void map_free(Map *m)
{
free(m);
}
int map_put(Map *m, int key, int value)
{
if (m == NULL || m->len >= CAP) {
return -1;
}
m->keys[m->len] = key;
m->vals[m->len] = value;
m->len++;
return 0;
}
int map_get(const Map *m, int key, int *out)
{
if (m == NULL) {
return -1;
}
for (int i = 0; i < m->len; i++) {
if (m->keys[i] == key) {
*out = m->vals[i];
return 0;
}
}
return -1;
}Common Gotchas
Dereferencing an opaque pointer in a client file is a compile error, and a cast to a guessed struct is a silent ABI break
stay on the functions.
The caller cannot put a `Map` on the stack
they put a `Map *` on the stack. The object itself comes from `map_new`.
Freeing the pointer with `free` instead of `map_free` skips internal cleanup
if the map owns keys, that is a leak.