NULL value

J

John Ratliff

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
 
A

Artie Gold

John said:
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
 
V

Victor Bazarov

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

Usually including said:
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
 
A

Alf P. Steinbach

* 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).
 
J

John Ratliff

Alf said:
* 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top