Thread: Cofusion regarding NULL macro

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    Cofusion regarding NULL macro

    Hi!
    I have a confusion regarding NULL macro.
    Since ascii value of NULL is 0 ,so instead of using 0 why we used NULL macro for comparison
    purpose.
    Is it due to logical reason or something else?

    Please explain it...

    Thanks

    Bargi

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    when you read the code

    p == NULL - you understand that here it suposed to be used the pointer comparison

    when you see
    p == 0 - you cannot be sure is this code suppose to be

    p == NULL
    or
    *p == 0

    Also when you write
    *p == NULL compiler will warn you that you comparing pointer value to non-pointer value...

    So using this macro helps to prevent different typing errors
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > So using this macro helps to prevent different typing errors
    There is no right answer.
    http://c-faq.com/null/nullor0.html

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    In C, NULL is often defined as ((void *)0) which makes it impossible to accidently do:

    int x = NULL;

    In C++ NULL cant be defined with a cast to void * because void * cant be converted to any other type automatically

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  3. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  4. Tweakable Radar...
    By DoraTehExploda in forum Game Programming
    Replies: 8
    Last Post: 06-07-2005, 10:49 AM
  5. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM