Let's suppose I am trying to write a function that creates a simple linked list in C.
The function is like this:
node* createLinkedList(int n) {
int i = 0;
node* head = NULL;
node* temp = NULL;
node* p = NULL;
for (i = 0; i < n; i++) {
//create isolated node
temp = malloc(sizeof(node));
printf_s("\nEnter the data for node number %i:", i);
scanf_s("%d", &(temp->data));
temp->next = NULL;
if (head == NULL) {
//if list is empty then make temp as first node
head = temp;
}
else {
p = head;
while (p->next != NULL)
p = p->next;
p->next = temp;
}
}
return head;
}
If I purposely delete the line temp = malloc(sizeof(node));
the program compile just fine but I get a runtime error (obviously).
My question is: Shouldn't the compiler warn me that I did not allocate memory for the temp
variable? Is it allocating that memory implicitly? If so, how?