Learning pointers can be tricky. Accessing them like arrays is pretty straight forward and easy to understand. Creating them can be tricky to understand because of the multiple ways & can be used. Casting them to anything seems to be really easy to do, and a way to break all sorts of things if not done carefully.
Two things I wasn't taught about was casting pointers to arrays and assignment of arrays. Pointers cast to basically any type, so presumably it should be an easy task to cast to an array type. As for assigning to arrays, this presumably will be possible since everything else is assignable unless const.
So something like the following would be so elegant for casting a pointer to an array type and assigning to an array.
int x = 1
int arr[2] = { 2, 3 };
int* ptr = new int[10];
// plus some initialization code for ptr
arr = &ptr[1]; // assign address of ptr[1] to arr
This didn't work, so before wasting anyone's time I wanted to make sure I understood the data I was working with. Values, and addresses primarily. So, I made a simple program to output some of the addresses and values. The output is just below:
Address of {type: int} &x = 0031FEF4; x = 1
Address of {type: int[2]} &arr = 0031FEE4; arr = 0031FEE4
Address of {type: int[2]} &arr[0] = 0031FEE4; arr[0] = 2
Address of {type: int[2]} &arr[1] = 0031FEE8; arr[1] = 3
Address of {type: int*} &ptr = 0031FED8; ptr = 008428C8
Address of {type: int*} &ptr[0] = 008428C8; ptr[0] = 2
Address of {type: int*} &ptr[2] = 008428D0; ptr[2] = 1
After making it clear to myself that I "knew" what values were where such as the address stored at arr I came here to figure out why arr = &ptr[1]; did not work.
Shouldn't arr be assignable? Can I not cast a pointer to an array in such a way?
int*andint [2]have different purposes (though the second one can decay into the first).std::begin(arr)