Consider this code:
int i;
int is[10]{};
unsigned char * p = reinterpret_cast<unsigned char*>(&i);
unsigned char * ps = reinterpret_cast<unsigned char*>(&is[0]);
p += sizeof(int);
ps += sizeof(int);
p += sizeof(int);
ps += sizeof(int);
unsigned char c = *ps;
If we believe that it ps
points to a representation object is[0]
, then according to the arithmetic rule of the pointer, the behavior is undefined in the last two lines of code.
However, if ps
it is also a pointer to an int array representation object is
, then the behavior is determined.
So my question is: is a pointer to an object representing an object also a pointer to an element of an object representing a complete object?
source
share