I was getting a little stuck on this and I needed some clarification on what is exactly going on. I would really appreciate if someone could help me out.
int i[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; int* pointy = &i[1][1]; int* copyPointy = pointy; *pointy = 100; pointy = &i[0][2]; cout << *pointy << endl; cout << *copyPointy << endl; I want to know what the following line represents/means :
int* copyPointy = pointy; I am trying to figure out why *copyPointy returns 100 rather than 3? If pointy points to copyPointy, and if the address of pointychanges when the statement pointy = &i[0][2]; is executed, then shouldn't the address of copyPointy change too, and thus the contents at that address?
pointypoints first to&i[1][1], and later to&i[0][2].pointycannot ever point tocopyPointyas it isn't aint**.pointnorcopyPointypoint to each other at any point(!). The type of a pointer to pointer tointwould beint**, those are bothint*.int a = 1; int b = a; a = 2;Would you expect printingbto print 2? Pointers aren't magic. They hold addresses. Initializing one with the address held in another doesn't mean a later change to the address held in either does anything to the other.