[LC++]Null or 0

Carlo Wood carlo at alinoe.com
Mon Feb 23 00:12:01 UTC 2004


On Sun, Feb 22, 2004 at 04:12:56PM +0100, Peter Poulsen wrote:
> I have read that we should use 0 over NULL when setting a pointer to
> "nothing".

I assume you basically want

  ptr = 0;
  assert( !ptr );

Then that is the most certain way.
Note that compilers treat 0 special: you are allowed to assign it to a pointer.
You'd not be able to do:

  ptr = 1;

because then '1' is treated as an int.

You should therefore not look at '0' as an int, but rather as the
single value of all types that represents 'false' when converted
to bool, and 0 when converted to an int if the latter is possible.

I do not know of any modern compiler that uses something else
than 0 for NULL - so this is rather theoretical.  If you like
you can use NULL for pointers and think of it as 'false' in
the same sense - it won't matter in practise (just check to
be sure, before launcing a spacecraft to Mars).

-- 
Carlo Wood <carlo at alinoe.com>



More information about the tuxCPProgramming mailing list