(e-mail address removed)-cnrc.gc.ca (Walter Roberson) writes:
You can extract the representation of the pointer by aliasing or
memcpy()ing it to an appropriately sized array of unsigned char.
Can you? I'm not completely awake yet, but I can't seem to find
a justification for that in C89.
Aliasing via a union: C89 says that it is only defined if the
two elements are part of a common prefix of identical element types.
Type punning via a pointer cast: C89 talks about casting pointers
to and from other types and the circumstances under which the
double-converted result will compare the same as the original.
It does say that when casting pointer types, that the new
pointer might not be valid if the new type pointed to has a
stricter alignment. As char has the least strict alignment
and unsigned char is promised to have the same alignment as
char, that clause could potentially be construed as implying
that the cast pointer to unsigned char would be valid: but
if one takes that interpretation, then the implication would
be that casting a pointer to double into a pointer to short int
into a pointer to short int is going to produce valid results
because short int has alignment requirements no stricter than double.
Any pointer may be converted to pointer to void, and pointer
to void must (in C89) have the same alignment and representation
as pointer to char, but as per the above, it doesn't follow that
the chars (or unsigned chars) are meaningfully accessible.
I seem to recall that unsigned char shall have no padding and
no trap states (but that that is not promised for char or signed char),
so doing the cast to pointer to unsigned char and pulling out the
unsigned char elements is not going to fault -- but is it promised
that the unsigned char values pulled out must be the same as the
bytes of the original pointer representation? Perhaps that is the
logical implication through a good sequence of reasoning, but is
there chapter & verse that ensures that it is so?