hi, I having some problem with a void * and tree structure.
i am sure tha my fuction make tree is ok, but my fuction print return
adress instead of values.
Main :
int main()
{
node *n = init(5);
affiche(n);
return 0;
}
structure :
typedef struct node
{
void *value;
struct node* right;
struct node* left;
}node;
init :
node* init(int h)
{
if(h == 0)
return NULL;
int v = 1;
node *n = malloc(sizeof(n ode));
n->value = &v;
printf("->%d \t", *((int*)n->value));
n->right = init(h-1);
n->left = init(h-1);
return n;
}
print :
void affiche(node *n)
{
if(n != NULL)
{
int *v = (int*)n->value;
affiche(n->left);
printf("-->%d \t",*v);
affiche(n->right);
}
}
the problem is not in the fuction init, because the fuction printf
return good values.
i think it was in the fuction "affiche"
please help me, thanks
i am sure tha my fuction make tree is ok, but my fuction print return
adress instead of values.
Main :
int main()
{
node *n = init(5);
affiche(n);
return 0;
}
structure :
typedef struct node
{
void *value;
struct node* right;
struct node* left;
}node;
init :
node* init(int h)
{
if(h == 0)
return NULL;
int v = 1;
node *n = malloc(sizeof(n ode));
n->value = &v;
printf("->%d \t", *((int*)n->value));
n->right = init(h-1);
n->left = init(h-1);
return n;
}
print :
void affiche(node *n)
{
if(n != NULL)
{
int *v = (int*)n->value;
affiche(n->left);
printf("-->%d \t",*v);
affiche(n->right);
}
}
the problem is not in the fuction init, because the fuction printf
return good values.
i think it was in the fuction "affiche"
please help me, thanks
Comment