This is how one defines a struct type:
struct Foo {
...
};
This can be used as follows:
struct Foo foo;
struct Foo *p;
Using typedef, we can create an equivalent and compatible type without having to use struct
everywhere.
typedef struct {
...
} Foo;
This can be used as follows:
Foo foo;
Foo *p;
When creating a self-referencing type (as snippets 1 and 2 do), you have to (directly or indirectly) use a struct type and not a "generic" type.
struct Foo {
struct Foo *next;
};
But nothing stops from also creating a "generic" type.
typedef struct Foo Foo;
(They can even have the same name, as is the case in my example.)
Snippets 1 and 2 are equivalent, creating both a struct type (struct list_s
) and an equivalent "generic" type (list_t
).
Snippet 3 creates only a "generic" type (other_t
).
The structure is probably used as the type for the nodes of a linked list. The pointer in one node would point to the next node in the list.
list_t *head list_t anon0 list_t anon1 list_t anon2
+------------+ +------------+ +------------+ +------------+
| ---------->| key: ... | +-->| key: ... | +-->| key: ... |
+------------+ +------------+ | +------------+ | +------------+
| next: -------+ | next: -------+ | next: NULL |
+------------+ +------------+ +------------+