Re: pointer doubt (1)

Discussion in 'C Programming' started by James Kuyper, May 8, 2013.

  1. James Kuyper

    James Kuyper Guest

    On 05/08/2013 02:46 PM, rashan wrote:
    > Can ne1 explain me the difference between.... a NULL pointer ... and a
    > VOID pointer.

    C is a case sensitive language; the term you're looking for is "null
    pointer". NULL is the name of a standard macro. For the same reason,
    there's no such thing as a VOID pointer (unless VOID has been given a
    meaning by something else, and you'll have to tell us what that meaning
    is before we can tell you what "VOID pointer" means). What you're
    looking for is "void pointer", or better, "pointer to void".

    "void" is a C keyword. It is used in contexts where it could be
    interpreted as a type, but it's used exclusively for the purpose of
    indicating that no actual type is relevant. In the case of a "pointer to
    void", what it means is a pointer to an unspecified type.

    A "null pointer constant" is a special phrase in the C language that
    does not mean what you might reasonably assume it means by looking at
    the individual words that make up the phrase. Not all constant
    expressions with a null pointer value qualify; and some of the things
    that do qualify are not null pointers at all, but rather integer
    expressions. They are, however, constant.

    What a "null pointer constant" actually is, is an integer constant
    expression (ICE) with a value of 0, or such an expression converted to
    the type void* ( Again, ICE is a term that doesn't mean what
    you might think by looking at the individual words. It would seem to
    mean "an expression with an integer type and a constant value", but in
    reality there are a lot of restrictions on what kinds of expressions can
    qualify as ICEs. See section 6.6p6 for details on those restrictions.

    A null pointer is what you get when a null pointer constant is converted
    to a pointer type. Note that "(void*)0" is both a null pointer constant
    in it's own right, and a null pointer constant (0) converted to a
    pointer type; it is therefore a null pointer. Null pointer constants get
    implicitly converted to pointers, and therefore null pointers, whenever
    used in certain kinds of operations where a pointer expression could
    also be used. For instance, &x==0, where x identifies an object or a
    function, causes the null pointer constant 0 to be implicitly converted
    to a null pointer of the same type as &x.

    The key important facts about null pointers are that all null pointers
    compare equal to each other, and that no null pointer compares equal to
    a pointer to any object or function. Therefore, &x==0 is guaranteed to
    be false. They are normally used as special pointer values - they
    indicate that the pointer does NOT, in fact, point at anything.

    NULL is a standard macro which is required to expand into a null pointer
    constant. In principle, it could be any null pointer constant: '\0', 0s,
    0, 0L, 0U, 0UL, 0LL, (3LL-'\03'), (2/3). However, in practice, I don't
    know any reason why any implementation would ever define it to be
    anything other than 0 or ((void*)0). The second form has an advantage,
    in that it can never be misused as an integer.
    James Kuyper, May 8, 2013
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    Chris Torek
    Feb 4, 2005
  2. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
  3. Replies:
  4. Peter Otten
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Terry Reedy
    Aug 10, 2013

Share This Page