Function-Pointer Methods
A function-pointer method is a field in a struct that holds the address of a function. Calling obj->draw(obj) selects the implementation at run time, which is how C does per-object behavior without classes.
Detailed Explanation
A prefix function shape_draw(s) is a static method: every Shape runs the same code. When two objects of the "same" type need different code — a circle draw versus a rectangle draw, a file logger versus a network logger — you store the function on the object.
struct Logger {
void (*write)(struct Logger *self, const char *line);
void *impl;
};
write is the method. impl is the private state, often a pointer to a more specific struct. The call site does not know which function is stored. It writes log->write(log, line);. You pass self explicitly because C will not.
This is a single-object strategy, not a class. Each instance can have a different function. Two loggers of the "same kind" share a function only if you installed the same one. A constructor for a file logger sets write to file_write and impl to a FILE *. A constructor for a test logger sets write to a function that appends to a buffer.
Compared with a vtable, function-pointer fields duplicate the pointers in every instance. That costs space if you have many objects and many methods. It is simpler when you have one or two operations and you want to swap one of them without building a type hierarchy. A state machine often uses one function pointer for "the current state's handler."
Lifetime is the hard part. The function must outlive the call. A pointer to a nested function is a GCC extension and is not portable. A pointer to a static function in the same file is fine. A pointer to a function in a plugin must remain valid until you unload the plugin, which you should not do while objects still point at it.
Code Examples
#include <stdio.h>
struct Logger {
void (*write)(struct Logger *self, const char *line);
FILE *fp;
};
static void logger_file_write(struct Logger *self, const char *line)
{
fputs(line, self->fp);
fputc('\n', self->fp);
}
static void logger_write(struct Logger *self, const char *line)
{
self->write(self, line); /* explicit self, as C requires */
}
int main(void)
{
struct Logger log = { .write = logger_file_write, .fp = stdout };
logger_write(&log, "ready"); /* prints: ready */
return 0;
}#include <stdio.h>
struct Ops {
int (*run)(int x);
};
static int twice(int x) { return x * 2; }
static int inc(int x) { return x + 1; }
int main(void)
{
struct Ops a = { twice };
struct Ops b = { inc };
printf("%d %d\n", a.run(21), b.run(41)); /* prints: 42 42 */
return 0;
}Common Gotchas
Calling a `NULL` method pointer is undefined behavior
constructors must install every required function, and callers must not zero the struct and use it.
Forgetting to pass `self` as the first argument is a type error if the prototype is right, and a crash if you cast it away
do not "simplify" the function type to drop the object.
Each object stores its own copies of the pointers
a struct with twelve methods wastes space. Use a vtable when many instances share one implementation.
The pointed-to function has to match the type exactly
a mismatch is undefined behavior, and a cast will hide it.