I've create this function that's supposed to create a randomly generated binary tree, it works fine but at the end of the function the root == NULL, i can't understand why!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define MAX_B 7
typedef struct _ramo{
int nbanane;
struct _ramo *dx;
struct _ramo *sx;
}ramo;
void creaAlbero(ramo *root, int n){
printf("%d\n",n);
root = malloc(sizeof(ramo));
root->nbanane=rand()%MAX_B;
printf("BANANA! %d\n",root->nbanane);
root->dx=NULL;
root->sx=NULL;
if ((int)(rand()%n)==0)
creaAlbero(root->dx, n+1);
if ((int)(rand()%n)==0)
creaAlbero(root->sx, n+1);
}
int main(){
srand((unsigned int)time(NULL));
ramo *root=NULL;
creaAlbero(root, 1);
if (root==NULL) {
printf("EMPTY!!");
}
return 0;
}
creaAlberoto modify yourrootobject inmain.