Re: dereferencing void *

Discussion in 'C Programming' started by Jun Woong, Jul 6, 2003.

  1. Jun Woong

    Jun Woong Guest

    "maxw_cc" <> wrote in message news:...
    >


    Please CROSS-POST if you think your article is subject to more than
    one group; as I recall, this is not the first time you double-post to
    comp.std.c and comp.lang.c.

    >
    > I've known and seen that dereferencing a void * is wrong.


    It's not wrong, it's just useless.

    [...]
    >
    > Looking at C90 or C99 I haven't
    > found an explicit prohibition (violation of a constraint)
    > for dereferencing a void *.
    >
    > So far the only justification generated from my interpretation
    > of the standard is the following:
    >
    > C99 6.5.3.2 paragraph 4
    > ...
    > If the operand has type `pointer to type',
    > the result has type `type'.
    > ...
    >
    > Then dereferencing a void * will generate a
    > a result that has `void' type.
    >
    > And then in 6.3.2.2
    >
    > 6.3.2.2 void
    >
    > 1 The (nonexistent) value of a void expression
    > (an expression that has type void) shall not
    > be used in any way, and implicit or explicit
    > conversions (except to void) shall not be
    > applied to such an expression...
    > ...
    >
    > Then joining these two parts I make the following
    > reasoning:
    >
    > 'Even though it's not illegal to dereference a void *
    > (it doesn't violate a constraint), dereferencing a void *
    > will produce a result that has void type, and this result
    > shall not be used in any way. So there's no practical or logical
    > reason for dereferencing a void *.'
    >
    > Is my reasoning correct?


    Yes.

    See this example:

    int i;
    void *pv = &i;

    pv; /* okay */
    *pv; /* okay */
    &*pv; /* wrong */

    pv = 0;
    pv; /* okay */
    *pv; /* wrong */

    Someone can argue that dereferencing a pointer to void is disallowed
    since the type "void" is not an lvalue (which designates an object),
    but the committee has already formed the authoritative interpretation
    in DR106 for C90, and I think it's not harmful than double-posting.


    --
    Jun, Woong ()
    Dept. of Physics, Univ. of Seoul
     
    Jun Woong, Jul 6, 2003
    #1
    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:
    580
    Jack Klein
    Apr 22, 2005
  2. Robert W Hand

    Re: dereferencing void *

    Robert W Hand, Jul 6, 2003, in forum: C Programming
    Replies:
    1
    Views:
    375
    maxw_cc
    Jul 7, 2003
  3. Stig Brautaset

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

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    828
    The Real OS/2 Guy
    Oct 28, 2003
  4. Replies:
    5
    Views:
    863
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    428
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page