I came across the following maze definition code:
typedef struct mazeNode {
int hasCheese;
int tag;
struct mazeNode *left;
struct mazeNode *right;
} maze_t;
maze_t maze = {
.tag = 1,
.left = &(maze_t) {
.left = &(maze_t) {
.left = &(maze_t) {},
.right = &(maze_t) {}
},
.right = &(maze_t) {
.right = &(maze_t) {}
}
},
.right = &(maze_t) {
.tag = 8,
.left = &(maze_t) {},
.right = &(maze_t) {
.tag = 10,
.left = &(maze_t) {
.tag = 11,
.left = &(maze_t) {
.hasCheese = 1,
.tag = 12
}
},
.right = &(maze_t) {}
}
}
};
From the linked blog post I understand that they are trying to define the binary tree with the cheese in the diagram.
However I can't seem to make head or tail out of what the C code is supposed to do. It would be great if someone could explain it to me.
.left = &(maze_t){};
and.right = &(maze_t){};
would more naturally be initialized to0
orNULL
. As it stands, there is an extra element in the hierarchy with a.tag
value of0
and a.hasCheese
value of0
too (and null left and right pointers). – Dryclean