0

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");
}
9
  • Head is uninitialised. Commented Jan 10, 2014 at 17:28
  • Downvote magnet detected: System.ALLCAPSEXCEPTION Commented Jan 10, 2014 at 17:29
  • 1
    @H2CO3 I take offense at your usage of a Java System constant to describe a question about the C language... Commented Jan 10, 2014 at 17:36
  • 2
    If you had declared head outside of any function, or had put the keyword static in 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). Commented Jan 10, 2014 at 17:46
  • 1
    @RonBurk: And we simply cannot afford the 500 trillionths of a second that would take! :-) Commented Jan 10, 2014 at 17:53

3 Answers 3

3

Memory is not initialized upon allocation. You cannot expect it to have a particular value until you set it.

Sign up to request clarification or add additional context in comments.

5 Comments

I thought that upon declaration it holds null value
Nope. I don't know what else to say... that's just not the case. It holds random data.
what i am supposed to do ?
Set it to NULL when you create it! head = NULL;
The initial value depends, among other things, on the storage class. Something that is at the top level of a compilation unit will be zero-initialized, but something that is declared in local scope in a function will not.
1

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");
}

Comments

1

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.

3 Comments

Do you really think he/she would notice a compiler warning?
hence he must not give this suggestion that turns out to be useful?
I compiled the code using codeblocks IDE , there were no warnings and i did not turn off warnings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.