NULL in C

Discussion in 'C Programming' started by Serve La, Oct 18, 2003.

  1. Serve La

    Serve La Guest

    C programmers get accused of writing platform dependant code when they write
    int *x = 0;
    They have to write
    int *x = NULL;
    because some platforms don't represent NULL as all bits zero.

    C++ programmers don't seem to have this problem, they can write 0 whenever
    they want. Why is that?
    Serve La, Oct 18, 2003
    #1
    1. Advertising

  2. Serve La

    Jirka Klaue Guest

    Serve La wrote:
    > C programmers get accused of writing platform dependant code when they write
    > int *x = 0;
    > They have to write
    > int *x = NULL;
    > because some platforms don't represent NULL as all bits zero.


    0 is a null pointer constant and is guaranteed to yield a null pointer
    when used in a pointer context, regardless of the actual bit pattern.

    Jirka
    Jirka Klaue, Oct 18, 2003
    #2
    1. Advertising

  3. Serve La

    xarax Guest

    "Serve La" <> wrote in message
    news:bmrjb2$nfv$1.nb.home.nl...
    > C programmers get accused of writing platform dependant code when they write
    > int *x = 0;
    > They have to write
    > int *x = NULL;
    > because some platforms don't represent NULL as all bits zero.
    >
    > C++ programmers don't seem to have this problem, they can write 0 whenever
    > they want. Why is that?


    Actually, NULL is supposed to be 0. Conforming C compilers
    recognize the value 0 (when used as a pointer) to be a
    special value that means NULL. Even when the implementation
    internally uses some other binary value to mean a NULL
    pointer, the value 0 is what is used the source code. This
    includes assignment, comparison, dereferencing, and casting
    between integer and pointer types. The compiler translates
    from 0 in the source code into its internal representation
    of a NULL pointer.

    This is why you can write:

    if(x)
    {
    // x is non-NULL
    }

    which is exactly the same as:

    if(NULL != x)
    {
    // x is non-NULL
    }

    which is also exactly the same as:

    if(0 != x)
    {
    // x is non-NULL
    }

    The C compiler knows that 0 is being used in a pointer-type
    expression and does the right thing.

    --
    ----------------------------------------------
    Jeffrey D. Smith
    Farsight Systems Corporation
    24 BURLINGTON DRIVE
    LONGMONT, CO 80501-6906
    303-774-9381
    http://www.farsight-systems.com
    z/Debug debugs your Systems/C programs running on IBM z/OS!
    xarax, Oct 18, 2003
    #3
  4. Serve La

    Mark Gordon Guest

    On Sat, 18 Oct 2003 14:53:03 GMT
    "xarax" <> wrote:

    > "Serve La" <> wrote in message
    > news:bmrjb2$nfv$1.nb.home.nl...
    > > C programmers get accused of writing platform dependant code when
    > > they write int *x = 0;
    > > They have to write
    > > int *x = NULL;
    > > because some platforms don't represent NULL as all bits zero.
    > >
    > > C++ programmers don't seem to have this problem, they can write 0
    > > whenever they want. Why is that?

    >
    > Actually, NULL is supposed to be 0.


    Actually it is not required to be 0. (void*)0 is also a valid definition
    of NULL.

    > Conforming C compilers
    > recognize the value 0 (when used as a pointer) to be a
    > special value that means NULL.


    Only a constant expression evaluation to 0. I don't think an integer
    containing a value 0 is guaranteed by the standard because it would be a
    pain to implement if the null pointer was not all bits zero.

    > Even when the implementation
    > internally uses some other binary value to mean a NULL
    > pointer, the value 0 is what is used the source code. This
    > includes assignment, comparison, dereferencing, and casting
    > between integer and pointer types. The compiler translates
    > from 0 in the source code into its internal representation
    > of a NULL pointer.


    <snip>

    the rest looked about right.
    --
    Mark Gordon
    Paid to be a Geek & a Senior Software Developer
    Although my email address says spamtrap, it is real and I read it.
    Mark Gordon, Oct 18, 2003
    #4
  5. On Sat, 18 Oct 2003 16:39:30 +0200, "Serve La" <> wrote:

    >C programmers get accused of writing platform dependant code when they write
    >int *x = 0;
    >They have to write
    >int *x = NULL;
    >because some platforms don't represent NULL as all bits zero.


    The accusers are misinformed. This is a common source of confusion. NULL and
    0 are interchangable with very few exceptions.

    >C++ programmers don't seem to have this problem, they can write 0 whenever
    >they want. Why is that?


    The main difference between C and C++ in this area is that in C++, NULL must
    be defined as

    #define NULL 0

    or equivalent. The alternative permitted in C

    #define NULL (void *)0

    is not acceptable in C++ because C++ does not allow implicit conversions
    from void * to other pointer types. The C++ people viewed these implicit
    conversions as a hole in the type system, and closed it.

    -- Mat.
    Mathew Hendry, Oct 18, 2003
    #5
  6. "Serve La" <> writes:
    > C programmers get accused of writing platform dependant code when they write
    > int *x = 0;
    > They have to write
    > int *x = NULL;
    > because some platforms don't represent NULL as all bits zero.
    >
    > C++ programmers don't seem to have this problem, they can write 0 whenever
    > they want. Why is that?


    The C FAQ is at <http://www.eskimo.com/~scs/C-faq/top.html>.
    Section 5 is about null pointers.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    Keith Thompson, Oct 18, 2003
    #6
  7. "Mathew Hendry" <> wrote in message
    news:...
    > On Sat, 18 Oct 2003 16:39:30 +0200, "Serve La" <> wrote:
    >
    > >C programmers get accused of writing platform dependant code when they

    write
    > >int *x = 0;
    > >They have to write
    > >int *x = NULL;
    > >because some platforms don't represent NULL as all bits zero.

    >
    > The accusers are misinformed. This is a common source of confusion. NULL

    and
    > 0 are interchangable with very few exceptions.

    int i = 0; /* Groovy */
    int j = NULL; /* An error on some compilers */

    :)

    --
    poncho
    Scott Fluhrer, Oct 18, 2003
    #7
  8. Serve La wrote:
    > C programmers get accused of writing platform dependant code when they write
    > int *x = 0;


    If they are so accused, it is by idiots.

    > They have to write
    > int *x = NULL;


    If they are so required, it is by idiots.

    > because some platforms don't represent NULL as all bits zero.


    This is not a reason and is completely irrelevant in pointer contexts.

    > C++ programmers don't seem to have this problem,


    Neither do C programmers.

    > they can write 0 whenever they want.


    As can C programmers.

    > Why is that?


    I don't know why your collection of at best half-truths exists. Your post
    is one of the worst attempts at a troll I've ever seen.



    --
    Martin Ambuhl
    Martin Ambuhl, Oct 18, 2003
    #8
  9. Serve La

    keanu Guest

    > I don't know why your collection of at best half-truths exists. Your post
    > is one of the worst attempts at a troll I've ever seen.


    Yours on the other hand is pritty good.
    keanu, Oct 18, 2003
    #9
  10. "Scott Fluhrer" <> wrote:

    >
    >"Mathew Hendry" <> wrote in message
    >news:...
    >> On Sat, 18 Oct 2003 16:39:30 +0200, "Serve La" <> wrote:
    >>
    >> >C programmers get accused of writing platform dependant code when they

    >write
    >> >int *x = 0;
    >> >They have to write
    >> >int *x = NULL;
    >> >because some platforms don't represent NULL as all bits zero.

    >>
    >> The accusers are misinformed. This is a common source of confusion. NULL

    >and
    >> 0 are interchangable with very few exceptions.

    >int i = 0; /* Groovy */
    >int j = NULL; /* An error on some compilers */
    >
    >:)


    Er, seems you're just joking, but anyway: the discussion was about
    0 vs. NULL in pointer contexts.

    int *i = 0; /* valid */
    int *j = NULL; /* more idiomatic variant */
    printf("%p",0); /* undefined behaviour */

    Regards
    --
    Irrwahn
    ()
    Irrwahn Grausewitz, Oct 18, 2003
    #10
  11. Serve La

    Micah Cowan Guest

    "Serve La" <> writes:

    > C programmers get accused of writing platform dependant code when they write
    > int *x = 0;
    > They have to write
    > int *x = NULL;
    > because some platforms don't represent NULL as all bits zero.
    >
    > C++ programmers don't seem to have this problem, they can write 0 whenever
    > they want. Why is that?


    Neither do C programmers. The fact that NULL is not represented
    as all bits zero is:

    1) Not limited to C. This is also true of C++.
    2) Not relevant to the initializations you have above.

    Where it becomes a problem is more in code such as:

    void **foo = calloc(NUM_ELEMS * sizeof *foo);
    /* ... Code that assumes that foo[0] through foo[NUM_ELEMS-1]
    are NULL pointers due to the all-bits-zero initialization from
    calloc()... */

    HTH,
    Micah
    Micah Cowan, Oct 19, 2003
    #11
  12. Serve La

    pete Guest

    Serve La wrote:

    > C programmers get accused of writing
    > platform dependant code when they write
    > int *x = 0;


    The accusers are ignorant.

    > They have to write
    > int *x = NULL;
    > because some platforms don't represent NULL as all bits zero.
    >
    > C++ programmers don't seem to have this problem,
    > they can write 0 whenever they want.


    > Why is that?


    Your question depends on an invalid hypothesis.

    --
    pete
    pete, Oct 19, 2003
    #12
  13. Serve La

    Serve La Guest

    "Martin Ambuhl" <> wrote in message
    news:_Jhkb.2609$...
    > > Why is that?

    >
    > I don't know why your collection of at best half-truths exists. Your post
    > is one of the worst attempts at a troll I've ever seen.


    Troll!?!? More misinformed than trolling and it was somebody here who said
    it BTW.

    Here's the worst troll. "Pascal is better than C"
    Serve La, Oct 19, 2003
    #13
  14. Serve La

    Joe Wright Guest

    Irrwahn Grausewitz wrote:
    >
    > "Scott Fluhrer" <> wrote:
    >
    > >
    > >"Mathew Hendry" <> wrote in message
    > >news:...
    > >> On Sat, 18 Oct 2003 16:39:30 +0200, "Serve La" <> wrote:
    > >>
    > >> >C programmers get accused of writing platform dependant code when they

    > >write
    > >> >int *x = 0;
    > >> >They have to write
    > >> >int *x = NULL;
    > >> >because some platforms don't represent NULL as all bits zero.
    > >>
    > >> The accusers are misinformed. This is a common source of confusion. NULL

    > >and
    > >> 0 are interchangable with very few exceptions.

    > >int i = 0; /* Groovy */
    > >int j = NULL; /* An error on some compilers */
    > >
    > >:)

    >
    > Er, seems you're just joking, but anyway: the discussion was about
    > 0 vs. NULL in pointer contexts.
    >
    > int *i = 0; /* valid */
    > int *j = NULL; /* more idiomatic variant */
    > printf("%p",0); /* undefined behaviour */
    >

    The variadic printf() requires (void*)0 because the compiler doesn't
    parse the format string and doesn't know the context of 0. If the
    implementation has defined 'NULL 0' then (void*)NULL will be required.
    --
    Joe Wright http://www.jw-wright.com
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Oct 19, 2003
    #14
  15. "Serve La" <> wrote:

    >"Martin Ambuhl" <> wrote in message
    >news:_Jhkb.2609$...
    >> > Why is that?

    >>
    >> I don't know why your collection of at best half-truths exists. Your post
    >> is one of the worst attempts at a troll I've ever seen.

    >
    >Troll!?!? More misinformed than trolling and it was somebody here who said
    >it BTW.


    If somebody here said so, he has most certainly been corrected.
    Or maybe you just misunderstood something.

    Regards
    --
    Irrwahn
    ()
    Irrwahn Grausewitz, Oct 19, 2003
    #15
  16. Joe Wright <> wrote:

    >Irrwahn Grausewitz wrote:

    <snip>
    >> 0 vs. NULL in pointer contexts.
    >>
    >> int *i = 0; /* valid */
    >> int *j = NULL; /* more idiomatic variant */
    >> printf("%p",0); /* undefined behaviour */
    >>

    >The variadic printf() requires (void*)0 because the compiler doesn't
    >parse the format string and doesn't know the context of 0. If the
    >implementation has defined 'NULL 0' then (void*)NULL will be required.


    That's the point.
    --
    Irrwahn
    ()
    Irrwahn Grausewitz, Oct 19, 2003
    #16
  17. In article <bmrjb2$nfv$1.nb.home.nl>,
    "Serve La" <> wrote:

    > C programmers get accused of writing platform dependant code when they write
    > int *x = 0;
    > They have to write
    > int *x = NULL;
    > because some platforms don't represent NULL as all bits zero.
    >
    > C++ programmers don't seem to have this problem, they can write 0 whenever
    > they want. Why is that?


    That's not a problem of the C language, that is a problem with people
    who don't understand it.

    Come back when you understand the difference between an integer zero, a
    value with a representation of all bits zero, a null pointer, a null
    pointer constant and the NULL macro.
    Christian Bau, Oct 19, 2003
    #17
  18. Micah Cowan <> writes:
    [...]
    > Neither do C programmers. The fact that NULL is not represented
    > as all bits zero is:
    >
    > 1) Not limited to C. This is also true of C++.
    > 2) Not relevant to the initializations you have above.


    To be precise, NULL is not *necessarily* represented as all bits zero.
    On many implementations, NULL is represented as all bits zero (which
    can make it more difficult to detect bugs caused by code that makes
    this assumption).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
    Keith Thompson, Oct 19, 2003
    #18
  19. Serve La

    Dan Pop Guest

    In <> Micah Cowan <> writes:

    >Neither do C programmers. The fact that NULL is not represented
    >as all bits zero is:


    NULL is a macro. Its usual representations are 0 or ((void *)0).
    If you mean null pointer, NULL is *not* a valid abbreviation.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Oct 20, 2003
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kivak Wolf
    Replies:
    2
    Views:
    10,059
    Kivak Wolf
    Jun 28, 2005
  2. vizlab
    Replies:
    3
    Views:
    4,172
    Michael Bar-Sinai
    Oct 17, 2007
  3. Replies:
    16
    Views:
    7,283
    Mike Schilling
    Oct 12, 2005
  4. Replies:
    5
    Views:
    26,486
    Mike Schilling
    Mar 29, 2006
  5. Bo Peng
    Replies:
    13
    Views:
    13,266
    Siemel Naran
    Jul 18, 2004
Loading...

Share This Page