I´m just playing around with C when I got a problem I can´t figure out.
I have a struct, lets say
typedef struct Node
{
void * data;
int id;
}Node;
And my program looks like
void Node_Init(Node *node)
{
node = malloc(sizeof(Node));
node->id = 5;
}
int main()
{
Node *node;
Node_Init(node);
printf("ID %d", node->id);
}
When I run this code and node->id gets printed I get a random number? It´s like node->id gets allocated on the stack and not the heap?
Because when I do Node *node; If I do Node *node = malloc(sizeof(Node));
It works, but If I remember correctly its not needed to do like that. Anyone can help me out saying why this happens?