Morris Keesan said:
But it's not
guaranteed to be work, because there's no requirement in the language
that (int *) and (int **) are compatible.
6.3.2.3 sub 7 ISO C99 (applies for explicit pointer casting conversions)
"A pointer to an object or incomplete type may be converted to a pointer to
a different
object or incomplete type. If the resulting pointer is not correctly aligned
for the
pointed-to type, the behavior is undefined. Otherwise, when converted back
again, the
result shall compare equal to the original pointer. ... "
Ok, they're convertible and he is converting back... Yes? So, there is no
need for them to have compatible representation, since they are convertible
and the conversion is "round-trip". As long as he doesn't have an alignment
failure, he only has to worry about the legalities of casting an "lvalue".
I don't believe there's any completely portable way of doing what you want,
but I'd be happy to be proved wrong.
What about Ben Bacarisse's code in the reply just above this one?
Although Ben stated that a conversion of a pointer of one type to void *,
and then from that void * to a different type is technically undefined, I
think the stuff below indicates otherwise.
A void * is a pointer to a byte (1). The spec. says a void * has the same
representation as a pointer to char (6.2.5 sub 26 ISO C99). All objects in
C map onto a byte or sequence of bytes (6.2.6.1 sub 2 ISO C99). All
pointers can be converted to a void * and back without loss (6.3.2.3 sub 1
ISO C99). Of course, the C standard redefined a byte from exactly 8-bits,
as "defined" by Bob Bemer and Hugh Ross during the 1960's in the course of
developing ASCII, to an addressable unit of storage ... (3.6 ISO C99) and
elsewhere effectively made a byte 8-bits or more for C.
(1)
http://groups.google.com/group/comp.std.c/msg/74f5906f22a23811?hl=en
I mean, if a void * is a pointer to a byte, and all objects must be
represented as bytes, then how can you convert one type to a void *, but not
technically be able to convert that void * to another type especially if
that other type can also be converted to a void * ? That doesn't make sense
to me. The only way I could understand that being the case is if the
representation of a pointer converted to a void * was different for each
type, but, as I see it, that'd violate 6.2.5 sub 26 of ISO C99... Wouldn't
it?
Rod Pemberton