const void *

Discussion in 'C Programming' started by conrad@lawyer.com, Sep 20, 2006.

  1. Guest

    What purpose does qualifying
    a pointer to void with const
    really serve? Before you can use what
    the pointer points to, you must cast
    it to the appropriate pointer type.
    Then, you may as well include the
    const qualification in the case. Being that
    the const qualification serves no point
    upon casting the pointer to void to some
    other pointer type.

    For example, take a look at 'free'.
    Why is the parameter type 'const void *'?
    What purpose is it really serving?

    --
    conrad
     
    , Sep 20, 2006
    #1
    1. Advertising

  2. wrote:
    > What purpose does qualifying
    > a pointer to void with const
    > really serve? Before you can use what
    > the pointer points to, you must cast
    > it to the appropriate pointer type.


    No, void pointers will be implicitly converted to other pointer types
    with the same or greater cv qualifications. This means that the
    following implicit conversions are valid:

    void* -> const void*
    void* -> char*
    void* -> const char*
    const void * -> const char*

    while the following are not:

    const void* -> void*
    const void* -> char*

    > Then, you may as well include the
    > const qualification in the case. Being that
    > the const qualification serves no point
    > upon casting the pointer to void to some
    > other pointer type.
    >
    > For example, take a look at 'free'.
    > Why is the parameter type 'const void *'?
    > What purpose is it really serving?


    So you can pass a const pointer without a cast.

    --
    Clark S. Cox III
     
    Clark S. Cox III, Sep 20, 2006
    #2
    1. Advertising

  3. wrote:
    > What purpose does qualifying
    > a pointer to void with const
    > really serve? Before you can use what
    > the pointer points to, you must cast
    > it to the appropriate pointer type.
    > Then, you may as well include the
    > const qualification in the case. Being that
    > the const qualification serves no point
    > upon casting the pointer to void to some
    > other pointer type.
    >
    > For example, take a look at 'free'.
    > Why is the parameter type 'const void *'?

    <snip>

    But it isn't. Quoth ISO/IEC 9899:1999:

    7.20.3.2 The free function

    Synopsis

    1 #include <stdlib.h>
    void free(void *ptr);


    > What purpose is it really serving?


    A const pointer as a function argument generally means something to the
    extent of "This function promises not to modify what the pointer points
    to."


    Mark F. Haigh
     
    Mark F. Haigh, Sep 20, 2006
    #3
    1. Advertising

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. Ollej Reemt
    Replies:
    7
    Views:
    600
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    836
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    880
    S.Tobias
    Jul 22, 2005
  4. Javier
    Replies:
    2
    Views:
    618
    James Kanze
    Sep 4, 2007
  5. 0m
    Replies:
    26
    Views:
    1,170
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page