I have this structure definition:
typedef struct node_bst
{
int data;
struct node_bst *lchild;
struct node_bst *rchild;
struct node_bst *parent;
} node_bst;
I tried to create a pointer to the structure using this:
node_bst *root;
and allocated memory to it like this:
root= malloc(sizeof(node_bst));
Now, in order to initialize the data items in it I was tried this statement (taking a cue from the usual initialization of structure variables):
*root= {0, NULL, NULL, NULL};
But the compiler threw off an error
error: expected expression before ‘{’ token
I looked it up and found that I need to typecast it like this:
*root= (node_bst) {0, NULL, NULL, NULL};
Now it works fine but my question is, why do i need to do this?
I expected that the compiler would already know that root is a pointer to node_bst type structure variable. So why the need to typecast the rvalue?
Another strange thing:
int *a= malloc(sizeof(int));
*a= 4;
This works just fine.