Null Pointer

This is a discussion on Null Pointer within the C Programming forums, part of the General Programming Boards category; Null pointer points to 0 or (void*)0 ?...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Location
    New Delhi,India
    Posts
    17

    Null Pointer

    Null pointer points to 0 or (void*)0 ?

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Both of those point to the same address. I think "(void*)0" is the correct definition of NULL.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I believe the reason for NULL to be declared as (void *)0 is two-fold:

    1. 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).
    2. 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.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MacGyver View Post
    I believe the reason for NULL to be declared as (void *)0 is two-fold:

    1. 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).
    2. 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: Memory allocated

    --
    Mats
    Last edited by matsp; 07-15-2008 at 04:39 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 10:07 AM
  2. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  3. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  4. Why am I getting these errors??
    By maxthecat in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2006, 12:00 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21