Re: cast to 'void *'



On 5 déc, 23:06, Keith Thompson <ks...@xxxxxxx> wrote:
blargg....@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) ks...@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"

I agree with you, nevertheless, new POSIX standard (issue 7) says:
" 2.12.3 Pointer Types
18881
All function pointer types shall have the same
representation as the type pointer to void.
18882
Conversion of a function pointer to void * shall not
alter the representation. A void * value
18883
resulting from such a conversion can be converted back to
the original function pointer type,
18884
using an explicit cast, without loss of information.
18885
The ISO C standard does not require this, but
it is required for POSIX conformance.
Note:
18886
"
Therefore, if you program a POSIX compliant application, the
conversion with an explicit cast is permitted.
.



Relevant Pages

  • Re: bug in visual studio .net 2003 - breakpoints and memcpy
    ... According to ANSI C and standard C++, there is no standard conversion from a ... function pointer to void*, and vice versa. ... Both Win32 and Posix define the conversion. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: bug in visual studio .net 2003 - breakpoints and memcpy
    ... >> from a function pointer to void*, ... it also says "Any type conversion not mentioned below and not ... So neither reinterpret_cast nor the explicit cast notation can be used to ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Some pointer quiestions again
    ... in any system on which one conversion preserves ... >>void pointer reliably, if not then there would appear to be no portable ... >>way to print the value of a function pointer. ...
    (comp.lang.c)
  • Re: Some pointer quiestions again
    ... > I have seen a proposal for a "%P" (uppercase P) conversion to print ... a function pointer, you can either cast it to void* and print it using ...
    (comp.lang.c)
  • Re: C5509a Idle Mode 3 + ISR,
    ... periodically and come out of Idle mode 3 whenever the RTC interrupt ... interrupt void INT0_isr; ...     configureRtc; ... int10   .ivec no_isr ...
    (comp.dsp)