what is the difference between NULL and 0

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iceman19860106

    what is the difference between NULL and 0

    hello everyone!
    what is the difference between NULL and 0 in C language?
  • Richard Heathfield

    #2
    Re: what is the difference between NULL and 0

    iceman19860106 said:
    hello everyone!
    what is the difference between NULL and 0 in C language?
    NULL is a macro defined in <stdio.h>

    It represents a null pointer constant, and may be defined either as 0 or as
    ((void *)0), which gives you a big hint that there's precious little
    difference between them in semantic terms. The most important difference
    is that 0 can definitely be used in contexts where an int is required,
    whereas the possibility that NULL may be defined as ((void *)0) deters the
    wary from using it in such contexts.

    Thus, given this:

    T *p = malloc(sizeof *p);

    these are equivalent:

    if(p != NULL)

    and

    if(p != 0)

    They do the same thing with the same meaning.

    But, given this:

    int x;

    these are not equivalent, or at least might not be:

    x = 0;

    and

    x = NULL; /* if NULL is ((void *)0), which it can be,
    this will cause a diagnostic message
    */

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • Mark McIntyre

      #3
      Re: what is the difference between NULL and 0

      iceman19860106 wrote:
      hello everyone!
      what is the difference between NULL and 0 in C language?
      This is a FAQ. Read section 5.

      --
      Mark McIntyre

      CLC FAQ <http://c-faq.com/>
      CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

      Comment

      • CBFalconer

        #4
        Re: what is the difference between NULL and 0

        iceman19860106 wrote:
        >
        what is the difference between NULL and 0 in C language?
        NULL is a macro defining a pointer. The pointer points nowhere,
        and thus cannot be dereferenced.

        0 is a number. It represents an integer one less than 1. It is
        usable in all integer expressions, except as a divisor.

        --
        [mail]: Chuck F (cbfalconer at maineline dot net)
        [page]: <http://cbfalconer.home .att.net>
        Try the download section.

        Comment

        • Jack Klein

          #5
          Re: what is the difference between NULL and 0

          On Wed, 29 Oct 2008 19:29:29 -0500, CBFalconer <cbfalconer@yah oo.com>
          wrote in comp.lang.c:
          iceman19860106 wrote:

          what is the difference between NULL and 0 in C language?
          >
          NULL is a macro defining a pointer. The pointer points nowhere,
          and thus cannot be dereferenced.
          The statement above is sloppy to the point of being incorrect, Chuck.

          NULL is a macro that represents a null pointer constant. It most
          certainly NOT a pointer, it is a value that can be assigned to a
          pointer object or compared against the value of a pointer object.

          NULL in a source file is no more a pointer than a numeric literal '0'
          is an int.
          0 is a number. It represents an integer one less than 1. It is
          usable in all integer expressions, except as a divisor.
          Still sloppy, but close enough.

          --
          Jack Klein
          Home: http://JK-Technology.Com
          FAQs for
          comp.lang.c http://c-faq.com/
          comp.lang.c++ http://www.parashift.com/c++-faq-lite/
          alt.comp.lang.l earn.c-c++

          Comment

          • Nick Keighley

            #6
            Re: what is the difference between NULL and 0

            On 30 Oct, 02:03, Jack Klein <jackkl...@spam cop.netwrote:
            On Wed, 29 Oct 2008 19:29:29 -0500, CBFalconer <cbfalco...@yah oo.com>
            <snip>
            0 is a number.  It represents an integer one less than 1.  It is
            usable in all integer expressions, except as a divisor.
            >
            Still sloppy, but close enough.
            zero is a number equal to the cardinality of the empty set?


            --
            Nick Keighley

            Comment

            Working...