Small (pedantic?) addition:

In C NULL is usually defined as (void *) 0

In C++ as 0

In C, implicit casting from one pointer type to another is never a
problem so

char *myCharPtr = NULL;

will not lead to errors with NULL defined as a void pointer. In C++,
implicit casting from one pointer type to another is not allowed, so the
above statement _would_ lead to an error were it not that

- in C++ NULL is defined as (the integer) 0 and
- in C++ (the integer) 0 has a special status so it can be used as a
pointer of any type as well as an integer.

That is all, regards.

Caspar

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Logan Shaw
Sent: 23 December 2004 10:19
To: Palm Developer Forum
Subject: Re: Difference between NULL and 0?

Andrew J. Huang wrote:
> This is a really pretty dumb question, but NULL and 0 act
> differently using the gnu toolchain.  What exactly is the
> difference?

Not necessarily a dumb question; it is one of the finer points of C,
since initially you just see lots of references to NULL and only
later start to consider what the relationship between 0 and NULL is
exactly.

Anyway, if you have a copy of the K&R book, look at section 5.4
(p. 102 in my copy).  There you will find the following:

        Pointers and integers are not interchangeable.  Zero is the
        sole exception: the constant zero may be assigned to a pointer,
        and a pointer may be compared with the constant zero.  The
        symbolic constant NULL is often used of zero, as a mnemonic
        to indicate more clearly that this is a special value for a
        pointer.  NULL is defined in <stdio.h>.

So, the answer is that NULL and zero should be the same in C.
I believe the same applies to C++.

So, if the GNU toolchain is treating them differently, that would
appear to be a problem with the toolchain.  However, I can imagine
that NULL could be #define'd as either "((void *) 0)" or just "(0)",
and I suppose the two might actually act slightly differently in
some cases[1].

For more information, you could consult the newsgroup comp.lang.c,
where you will find people who have seemingly devoted their lives
to knowing the correct answers to pedantic questions about C.  :-)

By the way, have you tried searching the header files for "NULL"
to see how your system defines it?  On Unix(-ish) systems, you
could probably find the answer with something like
"grep -w 'define.*NULL' *.h" or the equivalent
recursive-directory-searching incantation.

  - Logan

[1]  And there is always the possibility that rather than
     "#define NULL (0)" they might have used something like
     "static const void *NULL = 0;".

-- 
For information on using the Palm Developer Forums, or to unsubscribe,
please see http://www.palmos.com/dev/support/forums/

-- 
For information on using the Palm Developer Forums, or to unsubscribe, please 
see http://www.palmos.com/dev/support/forums/

Reply via email to