There are a lot of different ways to create a structure in C, and they're all actually a little bit different from one-another.
The basic way is to do something like this:
Code:
struct point {
int x;
int y;
};
struct point pt = {0, 0};
This creates pt as a struct of type "point", and sets the x and y members to zero.
Another way is using an anonymous struct:
Code:
struct {
int x;
int y;
} pt;
pt.x = 0;
pt.y = 0;
This method creates pt out of an anonymous struct, defined inside the pt declaration. In other words, it cuts out "struct point" part.
A more popular way uses typedef to create a new type:
Code:
typedef struct {
int x;
int y;
} point_t;
point_t pt;
pt.x = 0;
pt.y = 0;
This assigns the anonymous struct to the type point_t. By doing this you can treat point_t just like you would any other type to declare new point structs.
The problem with this last method is that members of the struct you are defining can't be of that type, because that type's definition isn't done yet. In other words, this is illegal:
Code:
typedef struct {
node left;
node right;
int value;
} node;
Because node is used before it's defined. The solution, then is to forego the anonymous struct, and use a named one instead. So the following will suffice, for example for a binary tree:
Code:
typedef struct node {
struct node left;
struct node right;
int value;
} node_t;
node_t parent_node;
node_t left_node;
node_t right_node;
parent_node.left = left_node;
parent_node.right = right_node;
And that's how you do trees in C.