NULL value

Discussion in 'C++' started by John Ratliff, Aug 26, 2005.

  1. John Ratliff

    John Ratliff Guest

    If I need a NULL value, should I define it myself, or include a standard
    header of some kind?

    I've been including <cstdio> whenever I need it, but is it just as good
    to do something like

    #define NULL (void *)0

    Thanks,

    --John Ratliff
     
    John Ratliff, Aug 26, 2005
    #1
    1. Advertising

  2. John Ratliff

    Artie Gold Guest

    John Ratliff wrote:
    > If I need a NULL value, should I define it myself, or include a standard
    > header of some kind?
    >
    > I've been including <cstdio> whenever I need it, but is it just as good
    > to do something like
    >
    > #define NULL (void *)0
    >
    > Thanks,
    >
    > --John Ratliff


    You have two options:

    #include <cstdlib>

    or, just use 0.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com (new post 8/5)
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
     
    Artie Gold, Aug 26, 2005
    #2
    1. Advertising

  3. John Ratliff wrote:
    > If I need a NULL value, should I define it myself, or include a standard
    > header of some kind?


    Usually including <cstdlib> takes care of that.

    > I've been including <cstdio> whenever I need it, but is it just as good
    > to do something like
    >
    > #define NULL (void *)0


    No, you mustn't do that. If you're compiling C++, you are allowed to do

    #define NULL 0

    but no (void*) nonsense.

    V
     
    Victor Bazarov, Aug 26, 2005
    #3
  4. * John Ratliff:
    > If I need a NULL value, should I define it myself,


    No.

    > or include a standard header of some kind?


    You don't need to, just write

    0


    > I've been including <cstdio> whenever I need it,


    If you just need the name NULL it would be more natural to use [cstddef],
    since it doesn't drag in some many other definitions.


    > but is it just as good to do something like
    >
    > #define NULL (void *)0


    It's not valid to redefine a standard macro, and besides, the definition in
    C++ isn't "(void*)0", but typically just "0": §18.1/4 note 180 "Possible
    definitions include 0 and 0L, but not (void*)0" (see §4.10/1 for details).

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 26, 2005
    #4
  5. John Ratliff

    John Ratliff Guest

    Alf P. Steinbach wrote:
    > * John Ratliff:
    >
    >>If I need a NULL value, should I define it myself,

    >
    >
    > No.
    >
    >
    >>or include a standard header of some kind?

    >
    >
    > You don't need to, just write
    >
    > 0
    >
    >
    >
    >>I've been including <cstdio> whenever I need it,

    >
    >
    > If you just need the name NULL it would be more natural to use [cstddef],
    > since it doesn't drag in some many other definitions.
    >
    >
    >
    >>but is it just as good to do something like
    >>
    >>#define NULL (void *)0

    >
    >
    > It's not valid to redefine a standard macro, and besides, the definition in
    > C++ isn't "(void*)0", but typically just "0": §18.1/4 note 180 "Possible
    > definitions include 0 and 0L, but not (void*)0" (see §4.10/1 for details).
    >


    Thanks. This is what I wanted to know.

    --John Ratliff
     
    John Ratliff, Aug 26, 2005
    #5
    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,075
    Kivak Wolf
    Jun 28, 2005
  2. vizlab
    Replies:
    3
    Views:
    4,212
    Michael Bar-Sinai
    Oct 17, 2007
  3. Replies:
    5
    Views:
    26,683
    Mike Schilling
    Mar 29, 2006
  4. moonhk
    Replies:
    5
    Views:
    544
    moonhk
    Oct 11, 2006
  5. Col
    Replies:
    1
    Views:
    627
    peter koch
    Apr 21, 2006
Loading...

Share This Page