I'm trying to return a struct by value to find the position of a node in a tree. However using a wrapper to simplify the function call returns the incorrect value.
Relevant code:
typedef struct {
uint16_t x;
uint16_t y;
} coordinate_t;
coordinate_t node_pos_(uint16_t x, uint16_t y, node_t *node, node_t *find) {
printf("%u, %u\n", x, y);
if (node == find) {
printf("found node at %u, %u\n", x, y);
coordinate_t coords;
coords.x = x;
coords.y = y;
return coords;
}
for (uint16_t i = 0; i < node->child_count; i++) {
node_pos_(x + i, y + 1, node->children[i], find);
}
}
coordinate_t node_pos(node_t *root, node_t *node) {
return node_pos_(0, 0, root, node);
}
int main() {
coordinate_t coords = node_pos(root, child2);
printf("coordinates of %s: %u, %u\n", child2->name, coords.x, coords.y);
return 0;
}
The output:
0, 0
0, 1
0, 2
1, 2
1, 1
found node at 1, 1
coordinates of child2: 2, 0