Re: cast to 'void *'



blargg.h4g@xxxxxxxxxxxxx (blargg) writes:
Harald van =?UTF-8?b?RMSzaw==?= wrote:
On Fri, 05 Dec 2008 14:02:57 -0600, blargg wrote:
Rolf Magnus wrote:
Mark wrote:
[...]
void *p;
int *x;

p = x;
x = p;

printf("p=%p, x=%p\n", p, (void *)x);
return 0;
}

When else is implicit conversion to 'void *' done, except assignment
like above written?

Basically everywhere except variable argument lists.

void* to/from function pointer is also not implicit.

Implementations commonly provide both implicit and explicit conversions
between void * and function pointers as extensions, and neither is
available in standard C.

Argh! My statement above was about as relevant as

void* to/from struct tm is also not implicit.

Apologies.

Except that conversion between void* and struct tm is a constraint
violation. There is no implicit conversion, so such a conversion
would have to done via a cast operator. C99 6.5.4p2-3 says:

Constraints

2 Unless the type name specifies a void type, the type name shall
specify qualified or unqualified scalar type and the operand shall
have scalar type.

3 Conversions that involve pointers, other than where permitted by
the constraints of 6.5.16.1, shall be specified by means of an
explicit cast.

Conversion between void* and a function pointer, as I just wrote
elsethread, violates no constraint, but its behavior is undefined (but
of course an implementation is free to define it).

Incidentally, it seems to me that paragraph 3 is superfluous; any case
where it would apply is already covered by other constraints.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
.



Relevant Pages

  • Re: gcc knows about malloc()
    ... If a correct prototype is visible, this expression (after the implicit ... actual conversion and simply yields the same pointer-to-function ... this value to type void *. ...
    (comp.lang.c)
  • Re: srand(time(NULL))
    ... conversion of void * to whatever_type * is implicit). ... you should cast it to void*. ...
    (comp.lang.c)
  • Re: srand(time(NULL))
    ... NULL can be assigned to any pointer type without casting (also the conversion of void * to whatever_type * is implicit). ... I think the casting of the return value of timeto unsigned int is unnecessary, since the return value is implicitly converted to the unsigned int argument. ...
    (comp.lang.c)
  • Re: gcc knows about malloc()
    ... If a correct prototype is visible, this expression (after the implicit ... actual conversion and simply yields the same pointer-to-function ... this value to type void *. ...
    (comp.lang.c)
  • Re: Concept of Statements and Expressions
    ... its expression has a function type. ... Later down this post, where you use the phrase "a class conversion", ... conversion to result in a void expression? ... to the issue of expression statement evaluation. ...
    (comp.lang.c)

Loading