Originally Posted by
oogabooga
That's one wacky data structure you've created there. Your error, besides the general insanity, is using i instead of j in your printf.
Thanks man. Don't call me wacko though. I was trying to create a tree like structure.
Fixing that small error it prints 123.
Code:
/*
* treeattempt1.h
*
* Created on: 21/06/2012
* Author: logan
*/
#ifndef TREEATTEMPT1_H_
#define TREEATTEMPT1_H_
#endif /* TREEATTEMPT1_H_ */
struct node {
int value;
struct element* elements;
};
struct element {
struct node* child_node;
};
Code:
/*
* treeattempt1.c
*
* Created on: 21/06/2012
* Author: logan
*/
#include <stdio.h>
#include "treeattempt1.h"
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[j].child_node->value);
}
return 0;
}