tags:

views:

288

answers:

4

In C++ (gcc,VS) is NULL considered the same as False. Or more importantly if in a logical statement what does NULL evaluate to. There were a number of other questions but none for C++ specifically.

For some reason looping with a NULL control for(;;) just freezes up the program indicating that there is something other than a NULL placed between the ;;. Note this code is something our professor believes to contain NULL values.

+19  A: 

I believe NULL is generally defined to be 0, which coincidentally evaluates to false when used as a boolean expression.

Mike Daniels
And anything that is not 0, evaluated to true
Arve
Kinda sorta ... the precompiler substitutes the four characters NULL with the single character 0. If this results in code that compiles as the integer 0, then what you say is correct.
John at CashCommons
John, it seems like you're splitting hairs. What distinction are you trying to make?
Rob Kennedy
He's wrong anyway: `1NULL1` isn't `101`. The preprocessor replaces the __token__ `NULL` with the token `0`.
MSalters
@Arve a stream in an invalid state is not 0, but evaluates to false, as can anything which overloads operator bool.
Pete Kirkham
Pete, Arve obviously meant that any *integer or pointer* that isn't zero evaluates as true. A stream object is neither.
Rob Kennedy
It isn't coincidental. The null pointer value is supposed to evaluate to false, and so is integer 0. All other pointer and integer values are supposed to evaluate to true.
David Thornley
You are both wrong, folks. It can also be `0L` (long) or any other zero constant integer value. If you split hairs, then please split them *really* :)
Johannes Schaub - litb
+6  A: 

In C++, NULL is #defined to 0 (and thus evaluates to false). In C, it is a void*, which also evaluates to false, but its type is not a numeric type. In the standard library (at least on GCC):

#ifndef __cplusplus
#define NULL ((void *)0)
#else   /* C++ */
#define NULL 0
#endif  /* C++ */
Barry Wark
In C it *could be* a `void *`, but not necessarily *is* a `void *`. It could easily be defined as `0` in C as well, just like in C++.
AndreyT
A: 

NULL on gcc on some platforms (Mac) is much more complicated than that. It tries to start using the nullptr thingy from next cxx rev. Even so it should still evaluate to false in a boolean context

pm100
NULL is used to denote the null pointer constant, which evaluates to false in a boolean context. It really doesn't matter how it's specifically defined.
David Thornley
+1  A: 

It seems that you're considering the following code:

for (;;) { }

That will loop forever because the condition term is empty. You might be tempted to refer to that as the null condition to indicate that there is no condition present, but as you and everyone else who has read your question can attest, it's a confusing phrase to use because it does not mean the same as this:

for ( ; NULL; ) { }

That won't loop at. It uses the macro NULL, which is equivalent to the integer constant zero, and zero in a Boolean context is treated as false. That's not "the null condition" because there is a condition there — a condition that is always false.

If you got this from your professor, and he is teaching a class in C++, then I suspect you misunderstood him, because this isn't something I'd expect a professor to get wrong, so go ask him to fill in the hole in your notes. If he's not a C++ professor, then maybe he simply tried to use a C++ example to illustrate some other topic. You might wish to send him a note about it so that he can clarify and present some different example in future lectures.

Rob Kennedy