i cannot figure out why NULL is not printed
#include "stdio.h"
#include "stddef.h"
typedef struct{
int info;
struct DEMO1* next;
} DEMO1;
int main()
{
int temp;
DEMO1 *head ;
if(head==NULL)
printf("NULL");
}
Memory is not initialized upon allocation. You cannot expect it to have a particular value until you set it.
NULL when you create it! head = NULL;Your problem is that you haven't initialized the head pointer to any value.. It just contains whatever bytes were stored there previously (unless the OS was nice and did some clean up). You need to initialize head to NULL if you would like that if-statement to evaluate to true:
int main()
{
DEMO1 *head = NULL;
if(head==NULL)
printf("NULL");
}
The real life lesson here is that your compiler probably had the ability to inform you that head was used before it was initialized. If you did not see such a message, then either you have a fairly poor compiler, or you have not asked the compiler to warn you about all possible problems. Find out how to get all the help your compiler is capable of offering and you could save a lot of time in your programming.
System.ALLCAPSEXCEPTIONheadoutside of any function, or had put the keywordstaticin front of it, then your assumption that it would be initialized to 0/NULL would have been correct. That default is not true for stack variables, mainly in the name of efficiency (unlike static variables, that possibly unneeded initialization would have to be done every single time the function was entered).