You need to initialize the elements of the structure after selecting it with malloc:
struct node {
int data;
struct node *next;
};
struct node *n1 = malloc(sizeof(struct node));
n1->data = 0;
n1->next = NULL;
If you want to initialize your structure in one step with default values, which can be convenient if it is much larger, use a static structure with these defaut values:
struct node def_node = { 0, NULL };
struct node *n1 = malloc(sizeof(struct node));
*n1 = def_node;
Alternatively, you can use the C99 syntax:
struct node *n1 = malloc(sizeof(struct node));
*n1 = (struct node){ 0, NULL };
, { 0 } , :
struct node *n1 = malloc(sizeof(struct node));
*n1 = (struct node){ 0 };