In the following example, the array is not accessed through its first element but by what is conceptually an alias of the array. Nevertheless, accordingly to C++17/[basic.lval]/8 the stored value of an object can be accessed through an unsigned char. So is it right to think that the following assertion will never fire?
void g(){
unsigned char s[]={'X'};
unsigned char (*pointer_to_array_s)[1] = &s;
unsigned char *alias_to_array_s =
reinterpret_cast<unsigned char*>(pointer_to_array_s);
//alias_to_array_s is not a pointer whose value point to c[0] because an array
//and its firts element are not pointer interconvertible see [basic.compound]
//*alias_to_array_s aliases s;
assert(*alias_to_array_s=='X'); //may fire?
}
The fact the alias_to_array_s
is not a valid pointer to the first element of s
is due to a subtlety introduced in C++17, see this Q&A.
Now let's suppose that I modify the array through the alias, can I retrieve this modification by directly accessing the array?
void g(){
unsigned char s[]={'X'};
unsigned char (*pointer_to_array_s)[1] = &s;
unsigned char *alias_to_array_s =
reinterpret_cast<unsigned char*>(pointer_to_array_s);
*alias_to_array_s='Y'; //UB?
assert(s[0]=='Y');//may fire?
}