On Sun, 28 Sep 2003 19:46:01 +0100, Mark Gordon
<sp******@flash-gordon.me.uk> wrote:
On Sun, 28 Sep 2003 12:51:19 -0400 (EDT)
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote:
<snip>
int array[1], *pointer = (char*)array;
Sorry, pete, I don't get it. Is the above undefined or
incorrect in some way?
array is an array of ints which is being cast to a char*
*pointer is a pointer to an int which is having a pointer to char
assigned to it.
IIRC you are not allowed to assign a pointer to char to another pointer
type without a cast and I don't think the conversion from int* -> char*
-> int* is guaranteed.
Correct you can't implicitly convert, and hence assign, pass, return,
or initialize without cast, char* to any other data pointer *except*
void*. (Plus qualification as needed; that's orthogonal to type.)
With cast, the result is guaranteed for char (or void). 6.3.2.3p7:
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
aligned57) for the
pointed-to type, the behavior is undefined. Otherwise, when converted
back again, the
result shall compare equal to the original pointer. <snip>
Plus the fact that every object (hence every valid object pointer)
must be sufficiently aligned for character types (and void pointers),
but this is not required for any other data type. And that nonnull
data pointers compare equal iff they point to the same object, or the
special and problematic case &just_past_a == &b, but that shouldn't
apply here.
- David.Thompson1 at worldnet.att.net