Interface Structs
An interface struct is a fat pointer: a pointer to object state plus a pointer to a table of functions that object supports. Any type that can fill the table can be passed where the interface is expected.
Detailed Explanation
A vtable hanging off a base struct says "this object is a shape." An interface struct says "this value can be used as a writer." The object does not have to embed a base. You build the interface at the call site, or in a constructor that knows both pieces:
struct Writer {
void *ctx;
int (*write)(void *ctx, const char *data, size_t len);
};
ctx is the receiver. write is the method. A file writer stores a FILE * in ctx and a function that calls fwrite. A buffer writer stores a struct Buf * and a function that appends. A function that wants to log takes struct Writer, not FILE *. The logging code never includes the file or buffer headers.
This is how Go interfaces and Rust trait objects look at the machine level: a data pointer and a method table. In C you write both by hand. The table can be a single function, as above, or a pointer to a const struct of functions when there are several operations (write, flush, close). Sharing a const table keeps each Writer value two pointers wide regardless of method count.
Lifetime is not in the type. Writer does not own ctx. If ctx is a stack buffer, the Writer must not outlive the function. If ctx is heap-allocated, someone else must free it. A richer interface adds void (*destroy)(void *ctx) and documents that writer_close calls it once.
Compared with embedding a base struct, an interface does not require offset-zero layout and does not force every implementer into one hierarchy. A type can export several interfaces — Writer, Reader — without multiple embedding. The cost is that you construct the fat pointer, and that ctx is void *, so the implementing function must cast it back to the real type with no check.
Code Examples
#include <stdio.h>
#include <string.h>
struct Writer {
void *ctx;
int (*write)(void *ctx, const char *data, size_t len);
};
static int file_write(void *ctx, const char *data, size_t len)
{
FILE *fp = ctx;
return (int)fwrite(data, 1, len, fp);
}
static int writer_puts(struct Writer w, const char *s)
{
return w.write(w.ctx, s, strlen(s));
}
int main(void)
{
struct Writer w = { .ctx = stdout, .write = file_write };
writer_puts(w, "hello\n"); /* prints: hello */
return 0;
}#include <stdio.h>
#include <string.h>
struct Buf {
char data[16];
size_t len;
};
static int buf_write(void *ctx, const char *data, size_t len)
{
struct Buf *b = ctx;
if (len > sizeof b->data - b->len) {
return -1;
}
memcpy(b->data + b->len, data, len);
b->len += len;
return (int)len;
}
struct Writer {
void *ctx;
int (*write)(void *ctx, const char *data, size_t len);
};
int main(void)
{
struct Buf b = { {0}, 0 };
struct Writer w = { &b, buf_write };
w.write(w.ctx, "ok", 2);
b.data[b.len] = '\0';
printf("%s\n", b.data); /* prints: ok */
return 0;
}Common Gotchas
`ctx` is untyped
the implementing function casts it. Installing the wrong pair of `ctx` and `write` is a silent type error.
The interface does not own the object
freeing `ctx` while a `Writer` still exists leaves a dangling method receiver.
A one-function interface can store the function in the struct; several functions belong in a shared `const` table
do not copy twelve pointers into every `Writer` value.
Calling `write` when it is `NULL` is undefined behavior
constructors must fill every required slot.