 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Quantum Guest
|
Posted: Tue Oct 10, 2006 9:10 am Post subject: NULL==0? |
|
|
Hi,
Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
Thank you,
Q |
|
| Back to top |
|
 |
Quantum Guest
|
Posted: Tue Oct 10, 2006 9:10 am Post subject: Re: NULL==0? |
|
|
Nick Keighley wrote:
| Quote: | Quantum wrote:
Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
yes, but, since text is an array it can never be NULL...
Quantum wrote:
Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
yes, but, since text is an array it can never be NULL...
|
Good point! I'm passing text[] into a function, however it is still
declared as a pointer.  |
|
| Back to top |
|
 |
Nick Keighley Guest
|
Posted: Tue Oct 10, 2006 9:10 am Post subject: Re: NULL==0? |
|
|
Quantum wrote:
| Quote: | Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
|
yes, but, since text is an array it can never be NULL...
--
Nick Keighley |
|
| Back to top |
|
 |
Quantum Guest
|
Posted: Tue Oct 10, 2006 9:10 am Post subject: Re: NULL==0? |
|
|
Papastefanos Serafeim wrote:
| Quote: | "Quantum" <noemail (AT) address (DOT) com> wrote in message
news:6NHWg.10854$Fx4.6732@newsfe1-gui.ntli.net...
Hi,
Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
Thank you,
Q
Although it's from the C faq it addresses your question
http://c-faq.com/null/index.html
Serafeim
|
Ok, thanks.
Q |
|
| Back to top |
|
 |
Papastefanos Serafeim Guest
|
Posted: Tue Oct 10, 2006 9:10 am Post subject: Re: NULL==0? |
|
|
"Quantum" <noemail (AT) address (DOT) com> wrote in message
news:6NHWg.10854$Fx4.6732@newsfe1-gui.ntli.net...
| Quote: | Hi,
Are these equivalent:
char text[];
if(text==NULL){}
if(text==0){}
Thank you,
Q
|
Although it's from the C faq it addresses your question
http://c-faq.com/null/index.html
Serafeim |
|
| Back to top |
|
 |
Bart Guest
|
Posted: Fri Oct 13, 2006 9:10 am Post subject: Re: NULL==0? |
|
|
jmcgill wrote:
| Quote: | Strictly speaking they are equivalent, but idiomatically, the use of the
NULL macro indicates that you are evaluating a pointer, and not just
evaluating some integer.
|
Idiomatically, I like to use if(!ptr) because it looks the same as if
it was a boolean, and it makes sense to me: "if not ptr" is like "if
there's nothing in ptr" or "if ptr is not pointing to anything".
| Quote: | Comparing to NULL and comparing to an integer value of zero are
equivalent in you example, but they are not equivalent everywhere NULL
may be used.
|
They are always equivalent. It's guaranteed by the standard.
| Quote: | This is because NULL does not require a typecast in order
to be passed as a parameter to a function that expects a pointer, but a
plain int would not necessarily satisfy the type requirements of the
function prototype.
|
NULL cannot be defined as anything else than zero in C++ so that is
incorrect. NULL is the same as plain 0 as far as overloading is
concerned.
| Quote: | You might still get away with this, because all
your target platforms probably really do have a NULL pointer that is an
integer value with "all bits off", and all your target platforms
probably really do represent the integer value of zero as a two's
complement number with "all bits off." But, this is really just a happy
coincidence. What happens on a platform where "zero" is something else?
How about on a one's complement system where there are TWO zeros? And
what happens on a platform where the NULL pointer, while represented in
the high level language as "zero", really is some other value, like the
address of some hardware trap with a segment and offset value, different
for each program?
|
You're confusing stuff. In a C++ program the constant 0 IS the null
pointer. It doesn't matter what the representation of it is in the
hardware. If you use 0 in a pointer expression it will have the correct
representation. This is NOT the same as memcmp-ing with zero or
memset-ing a pointer to zero.
| Quote: | So, this doesn't happen on x86, Sparc, ARM, MIPS, PPC, 68K, or any other
machine you are likely to be using. And maybe it's irrelevant to ask if
you'd bet your life on it remaining so. But it is nonetheless
appropriate to practice the good habit of using the NULL macro for the
null pointer.
|
Talk for yourself. IMO, it is not a good habit to use any unnecessary
macro, which NULL is. NULL is a historical accident like several other
things in the language.
Regards,
Bart. |
|
| Back to top |
|
 |
Clark S. Cox III Guest
|
Posted: Sun Oct 15, 2006 6:30 am Post subject: Re: NULL==0? |
|
|
Old Wolf wrote:
| Quote: | Frederick Gotham wrote:
The macro, NULL, must be a compile-time integer constant which evaluates to
zero. No more. No less.
Not true. It could be anything as long as it can be converted
to a pointer, resulting in a null pointer.
For example, GCC defines NULL as __nullptr, which is a value
that meets this requirement. This definition does not break
any standards conformance.
|
__nullptr in GCC is still a constant integer expression that evaluates
to zero. Were it not, it could not be a null pointer constant.
--
Clark S. Cox III
clarkcox3 (AT) gmail (DOT) com |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|