Structs as Objects
C has no class keyword. An "object" is a struct plus the functions that take a pointer to it as their first argument. The type is the data. The functions are the methods.
Detailed Explanation
Java and C++ attach methods to a type. C attaches nothing. You recover the same shape by convention. A struct Counter { int value; }; is the instance data. counter_init, counter_bump, and counter_get are the methods. The first parameter is struct Counter *self, or const struct Counter *self when the method does not mutate. Callers write counter_bump(&c); instead of c.bump().
The convention is the whole design. Name the functions with a type prefix so they do not collide and so readers can grep. Put the struct in a header if the layout is public, or hide it behind an opaque type if it is not. Keep construction and destruction paired: every counter_init has a counter_destroy if anything was allocated.
Why bother? Because the problems objects solve — grouping state, protecting invariants, giving a value a lifetime — show up in C the moment a program is larger than one file. A bag of loose ints and a comment is how those invariants rot. A struct with a small set of functions is how they stay in one place. You do not get inheritance, visibility keywords, or dynamic dispatch unless you add them. You do get something a debugger can print and a reviewer can name.
Passing the struct by value makes a copy of the data and none of the "methods." There is no vtable hiding in a by-value struct unless you put a function-pointer table in it. Most object-style C uses pointers: cheap to pass, obvious to mutate, and able to outlive the function that created the object when the storage is allocated.
This style is how FILE *, DIR *, and most POSIX handles already work. You never call a "method" on a FILE with .. You call fread(buf, 1, n, fp). The object is the last argument there for historical reasons. New code usually puts it first.
Code Examples
#include <stdio.h>
struct Counter {
int value;
};
static void counter_init(struct Counter *self, int start)
{
self->value = start;
}
static void counter_bump(struct Counter *self)
{
self->value += 1;
}
static int counter_get(const struct Counter *self)
{
return self->value;
}
int main(void)
{
struct Counter c;
counter_init(&c, 40);
counter_bump(&c);
counter_bump(&c);
printf("%d\n", counter_get(&c)); /* prints: 42 */
return 0;
}#include <stdio.h>
#include <stdlib.h>
struct Counter {
int value;
};
static struct Counter *counter_new(int start)
{
struct Counter *c = malloc(sizeof *c);
if (c == NULL) {
return NULL;
}
c->value = start;
return c;
}
static void counter_free(struct Counter *c)
{
free(c);
}
int main(void)
{
struct Counter *c = counter_new(0);
if (c == NULL) {
return 1;
}
c->value = 3;
printf("%d\n", c->value); /* prints: 3 */
counter_free(c);
return 0;
}Common Gotchas
There is no implicit `this`
forgetting to pass `self` is a compile error, not a mysterious method-resolution failure. That is a feature.
A public struct has public fields
nothing stops `c.value = -1`. If the invariant matters, hide the layout with an opaque type.
Copying the struct copies the fields, including owned pointers
two "objects" then share one allocation. Either treat the type as a value with no owned memory, or forbid copies and provide an explicit clone.
Calling methods on an uninitialized struct is undefined behavior
pair every use with `init` or `new`.