As a newbie in C++ I've been playing around with pointers a bit. I have written the following code to interpret a short array as an integer:
#include <iostream>
int main(){
short array[2] = {10, 9};
short* pointer = array;
std::cout << pointer << ": " << *pointer << std::endl;
// 0xffffcbdc: 10
pointer++;
std::cout << pointer << ": " << *pointer << std::endl;
// 0xffffcbde: 9
int* pointer2 = (int*) array;
std::cout << pointer2 << ": " << *pointer2 << std::endl;
// 0xffffcbdc: 589834
}
Why is the value of the integer 589'834 (0009 000A) and not 655'369 (000A 0009)?
From the printed pointer addresses it looks like the array is in order in memory, why does casting to integer change that?