retrieving a value from nested struct gives negative values
Code:
/*
* treeattempt1.c
*
* Created on: 21/06/2012
* Author: logan
*/
#include <stdio.h>
struct node {
int value;
struct element* elements;
};
struct element {
struct node* child_node;
};
int numbers[3]={1,2,3};
int main() {
// create a tree with three branches
struct element branches[3];
struct node tree;
tree.elements=&branches[0];
// before we populate tree we need to point each child nodes to root of the tree
struct node childs[3];
int m=0;
for (m=0; m<3; m++) {
tree.elements[m].child_node=&childs[m];
}
// fill tree with numbers 1, 2, 3
int i=0;
for (i=0; i<3; i++) {
tree.elements[i].child_node->value=numbers[i];
}
// try printing out values in the tree
int j=0;
for (j=0; j<3; j++) {
printf("%d", tree.elements[i].child_node->value);
}
return 0;
}
This gives three negative values when I expect values 1, 2, 3. Any help will be appreciated!