I expected target to change to 0.
But it didn’t.
Why doesn’t target change?
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
int array[3] = {1, 2, 3};
int target = 127;
cout << array[0] << endl;
cout << array[1] << endl;
cout << array[2] << endl;
cout << target << endl;
cout << "Working on.." << endl;
int* arrp = array;
arrp = arrp + 3;
*arrp = 0;
cout << array[0] << endl;
cout << array[1] << endl;
cout << array[2] << endl;
cout << target << endl;
return 0;
}
I expected target to change to 0.
Because I thought target is stored right next to array[2].
So if I change pointer arrp + 3, the target should be changed.
I wonder if my thinking is right.
New contributor
Byungha Park is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1