Cast pointer to fixed-size array in return statement
arrays, c++
Solution
I find that array types are easier to deal with with a typedef:
typedef int ints[3];
Then your `as_array_ref` must be written so that `&as_array_ref() == &x`.
The following syntaxes are possible:
plain C-style cast from `int*` to `ints*`:
`ints& as_array_ref() { return *( (ints*)(&x) ); }`
C++ style `reinterpret_cast` (suggested by @Mike Seymour - see also his answer). It is often considered a better practice in C++:
`ints& as_array_ref() { return *reinterpret_cast<ints*>(&x); }`
Cast from `int&` to `ints&` which is slightly shorter but (for me) less intuitive:
`ints& as_array_ref() { return reinterpret_cast<ints&>(x); }`
Problem
The simplest way to ask this question is with some code: ``` struct Point { int x; int y; int z; int* as_pointer() { return &x; } // works int (&as_array_ref())[3] { return &x; } // does not work }; ``` `as_pointer` compiles, `as_array_ref` does not. A cast seems to be in order but I can't figure out the appropriate syntax. Any ideas?