Quick question, how do I forward declare the following treeNodeListCell
struct.
You don't need to.
You must in the first place discriminate between identifying a structure type by tag and identifying it via a typedef
ed alias. And in particular, you need to appreciate that typedef
is completely optional. Where you use it to define aliases for structure types, it may be more clear to separate the typedef
declaration from the structure declaration.
Here are your declarations without any typedef
:
struct _treeNode {
struct _treeNodeListCell *next_possible_positions;
};
struct _treeNodeListCell {
struct _treeNode *node;
struct _treeNodeListCell *next;
};
No forward declarations are required for structure types expressed in struct <tag>
form.
You may add typedefs, too. They can be associated with the definitions above by adding the typedef
keyword and one or more identifiers, or they may simply be written separately, as I previously recommended:
typedef struct _treeNode treeNode;
typedef struct _treeNodeListCell treeNodeListCell;
Personally, I think typedefs are much overused. I usually do not define typedef
aliases for my structure and union types.
BUT if you really want to do then you can declare a typedef of an incomplete type, such as an as-yet undefined structure type. This is a regular declaration, not a forward declaration, but it would allow you to use the aliases in the definitions of the structures, which I take to be your objective:
typedef struct _treeNode treeNode;
typedef struct _treeNodeListCell treeNodeListCell;
struct _treeNode {
treeNodeListCell *next_possible_positions;
};
struct _treeNodeListCell {
treeNode *node;
treeNodeListCell *next;
};
In fact, starting in in C11, you can write multiple declarations of the same typedef
name in the same scope, as long as they all define the name to identify the same type. This provision can be leveraged to allow the typedef / structure declarations presented in the question to compile. Note well that designating the same type does not require the type to be expressed the same way. Since this is supposed to be an exercise, I will leave it to you to work out the few remaining details.