Are char*
, int*
, long*
or even long long*
of same size (on a given platform)?
There is no such guarantee in either C or C++ ISO standards, but in practice, I've yet to see a platform where this doesn't hold.
Note that regardless of this, reinterpret_cast
'ing one pointer to another will more often than not lead to U.B., with a few exceptions (void*
, and unsigned char*
for PODs). So would any union tricks. So the obvious question is: why would you care?
They're not guaranteed to be the same size, although on the platforms I have experience with they usually are.
C 2011 online draft:
6.2.5 Types
...
28 A pointer tovoid
shall have the same representation and alignment requirements as a pointer to a character type.48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.
48) The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.