Reputation: 523
I'm analysing this bit of code on which I am to build a doubly linked list:
struct dplist_node {
dplist_node_t * prev, * next;
element_t element;
};
struct dplist {
dplist_node_t * head;
// more fields will be added later
};
In an included header file I found the following lines:
typedef int element_t;
typedef struct dplist dplist_t;
typedef struct dplist_node dplist_node_t;
What I gather from this is that dplist_node_t is a typedef of the struct dplist_node but there are still pointers defined to dplist_node_t (and thus dplist_node) in both structs.
I find all of this very confusing and can't seem to unravel what is an element of which struct or what pointer is pointing where. Can anyone help me understand?
Upvotes: 1
Views: 54
Reputation: 50911
Actually this equivalent declaration is easier to understand:
struct dplist_node {
struct dplist_node * prev, * next;
element_t element;
};
struct dplist {
struct dplist_node * head;
// more fields will be added later
};
the typedef
adds only confusion in this case.
With:
typedef struct dplist dplist_t;
you can write dplist_t
instead of struct dplist
, or dplist_t*
instead of struct dplist*
.
Illustration
struct dplist_node {
struct dplist_node * prev, * next;
element_t element;
};
Here it is clear that prev
and next
are pointers to the struct dplist_node
itself.
Original declaration:
struct dplist_node {
dplist_node_t * prev, * next;
element_t element;
};
when we see this, we dont know what dplist_node_t
is unless we look into the header file where dplist_node_t
is defined as being the equivalent of struct dplist
. So IMO it's less clear.
Upvotes: 1