Null pointer points to 0 or (void*)0 ?
Printable View
Null pointer points to 0 or (void*)0 ?
Both of those point to the same address. I think "(void*)0" is the correct definition of NULL.
I believe the reason for NULL to be declared as (void *)0 is two-fold:
- Allows NULL to be a pointer type so the compiler can tell if the user is using NULL properly (ie. int x = NULL; will produce a warning).
- NULL might not be 0 on some systems, hence the cast from 0 to void * allows the compiler to do any required things to the 0 before continuing.
And as I pointed out in the other post, the C++ standard actually accepts 0 the same as NULL, but C "requires" the (void *) cast to ensure it's treated as a pointer for the above reasons.
Edit: See also: http://cboard.cprogramming.com/showthread.php?t=105173
--
Mats