Size of null

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Time
    New Member
    • Jan 2010
    • 77

    Size of null

    When i did printf("%d",siz eof(NULL));
    answer is coming as 2 in Turbo C... Sizeof integer is also coming as 2... We cannot assume that null is of same size as integer.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    In that particular context yes but that might not be true for every context. Printing sizeof NULL is not very useful for understanding it you should really consider how it is defined, for C

    #define NULL ((void*)0)

    for C++

    #define NULL 0

    So for C++ it does appear that NULL is an integer. However you are correct that you should not assume that NULL has the same size as int. NULL is meant to be used in a pointer context, there where 32bit platforms that had 16bit integers and 32bit pointers and current 64bit platforms tend to have 32bit integers and 64bit pointers so NULL that is a pointer value very clearly does not have the same size as an int.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      NULL is a value that can be stored in variables of certain types. The sizeof operator is not intended to be used with values -- it is meant to be used with types. If you want to know how large a data pointer can be then you should obtain the size of a pointer type. A convenient choice is
      Code:
      sizeof(void*)

      Comment

      • Time
        New Member
        • Jan 2010
        • 77

        #4
        If it is really meant to be used with values and not types; it should have given error accordingly.. but this is not the case.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by Time
          If it is really meant to be used with values and not types; it should have given error accordingly.. but this is not the case.
          This is not the only place where C gives you enough rope to hang yourself.

          Comment

          • Time
            New Member
            • Jan 2010
            • 77

            #6
            So being 'expert' in this forum; you must have made many attempts by now to hang up your-self with that rope provided by C :)

            Comment

            Working...