I'm making a simple binary tree. I have a function that allocates memory for a root node and returns a pointer to that memory. I was careless and forgot to return the pointer at the end of the function. The thing is, that when I tested the function in main, somehow the pointer was actually returned. The code is:
struct node
{
int key;
struct node* lc;
struct node* rc;
};
struct node* init_root(int key)
{
struct node *root;
root = (struct node*)malloc(sizeof(struct node));
root->key = key;
root->lc = NULL;
root->rc = NULL;
}
int main()
{
struct node* root;
root = init_root(60);
printf("%d\n", root->key);
return 0;
}
Why the correct value I want to print is printed even if I dont return explictly the pointer?