Reputation: 53
How do I forward declare the following treeNodeListCell
struct?
I tried writing struct treeNodeListCell
before the struct definitions and the code doesn't compile.
Does anyone have an idea?
struct treeNodeListCell;
typedef struct _treeNode {
treeNodeListCell *next_possible_positions;
} treeNode;
typedef struct _treeNodeListCell {
treeNode *node;
struct _treeNodeListCell *next;
} treeNodeListCell;
Upvotes: 5
Views: 4529
Reputation: 34829
You can forward declare a struct
, but when you do, you need to use the struct
keyword with the forward-declared struct
tag.
struct _treeNodeListCell;
typedef struct _treeNode {
struct _treeNodeListCell *next_possible_positions;
} treeNode;
typedef struct _treeNodeListCell {
treeNode *node;
struct _treeNodeListCell *next;
} treeNodeListCell;
The alternative is a forward-declared typedef
. C allows you to typedef
an incomplete type, which is to say that you can typedef
the structure before defining the structure. That allows you to use the typedef in the structure definition.
typedef struct _treeNodeListCell treeNodeListCell;
typedef struct _treeNode {
treeNodeListCell *next_possible_positions;
} treeNode;
struct _treeNodeListCell {
treeNode *node;
treeNodeListCell *next;
};
If you want to use the structures in the question without changing them, all you need is the typedef
before the structure definitions.
typedef struct _treeNodeListCell treeNodeListCell;
typedef struct _treeNode {
treeNodeListCell *next_possible_positions;
} treeNode;
typedef struct _treeNodeListCell {
treeNode *node;
struct _treeNodeListCell *next;
} treeNodeListCell;
Upvotes: 6
Reputation: 180181
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.
Upvotes: 0
Reputation: 75062
You cannot omit struct
in C.
You should use
struct treeNodeListCell *next_possible_positions;
instead of
treeNodeListCell *next_possible_positions;
Upvotes: -3