what is the real meaning of NULL?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gobi Sakthivel
    New Member
    • Jan 2013
    • 26

    what is the real meaning of NULL?

    Code:
    int main()
    {
        int* a= NULL;
        return 0;
    }
    Here Integer pointer is assigned to NULL, I dont know
    the complete meaning of NULL till now. I am assuming tat a particular memory location is named as "NULL" and the pointer's which are assigned to NULL, points to tat particular location. Can anyone clear this basic doubt for me.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    NULL is a macro, normally defined to ((void*)0) in C and plan 0 in C++.

    NULL is a convenience to aid with the requirements of the language (C or C++). The language standards require that the value 0 when used in a pointer context is to be interpreted as and converted to the platforms NULL pointer value, that is a pointer that points nowhere.

    Note there is no requirement that the platforms NULL pointer value actually has the value 0 although for a vast majority of platforms this is the case. The requirement is only when the compiler sees a 0 in a pointer context in the code it is converted to whatever value the platform does use for a NULL pointer; obviously an easy conversion if the platforms NULL pointer value is 0.

    A NULL pointer nominally points no-where and trying to access what it points at is undefined behaviour and often, if you are lucky, produces some sort of memory exception or segmentation fault.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Notice that NULL is different than null. The first is a pointer value (as described earlier by Banfa); the second is a character value.

      Comment

      • swapnali143
        New Member
        • Mar 2012
        • 34

        #4
        NULL is Macro Defined in C....
        NULL is used as Constant......
        Its used to terminate String...
        Things to remember about NULL are.
        NULL is NOT EQUAL to 0
        and NULL is NOT EQUAL to '\0'

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          @donbock : I believe that the character is actually called nul or NUL (control characters tend to have 2 or 3 letter names)

          @swapnali143 Well yes strictly speaking you are correct by the standard NULL is defined to an implementation defined null pointer value.

          However in every compiler I have ever used (which is quite a few) NULL has always been defined to the value 0 or (void*)0 (depending on language) which works because of the requirement for the compiler to convert 0 used in a pointer context to the implementations null pointer value. That said you should of course never use NULL where you mean the value 0, it should only ever be used in the context of testing or assigning a pointer to the null pointer value.

          Additionally what you say about terminating a string is wrong. NULL is not used to terminate a string; as you point out it can not be assumed to have the value 0 and the value 0 is specifically what you want when terminating a string. Strictly the character '\0' (or NUL but this is the name of the character '\0' not a defined constant) is used to terminate a zero terminated string which is what C uses.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            @banfa You're correct -- I was wrong to say null means anything in particular. The C Standard uses that word in the following contexts.
            • null character: '\0'
            • the null wide character: L'\0'
            • null byte (within a multibyte character array)
            • null-terminated
            • null pointer constant: 0 or (void*)0 or NULL
            • null pointer: a null pointer constant cast to a pointer type
            • null statement: just a semicolon (;)
            • null preprocessing directive: just a hash (#)


            The term NUL comes from the ASCII specification not the C Standard. It is the name of the ASCII character that is encoded as 0.

            Comment

            Working...