1

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?

4
  • pointy points first to &i[1][1], and later to &i[0][2]. pointy cannot ever point to copyPointy as it isn't a int**. Commented Mar 13, 2015 at 6:24
  • Sorry, I meant doesn't *if copyPointy points to pointy then if the address of pointy changes, when pointy = &[0][2], shouldn't the address of copyPointy change too, and thus the contents at that address? Commented Mar 13, 2015 at 6:26
  • That doesn't really change anything, neither point nor copyPointy point to each other at any point(!). The type of a pointer to pointer to int would be int**, those are both int*. Commented Mar 13, 2015 at 6:28
  • 3
    int a = 1; int b = a; a = 2; Would you expect printing b to 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. Commented Mar 13, 2015 at 6:29

3 Answers 3

6

It's important to keep in mind that a pointer's value is an address - the address of the thing it's pointing to. What a pointer's value is and what it points to are two separate things.

Let's label your lines:

int i[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; int* pointy = &i[1][1]; int* copyPointy = pointy; // (1) *pointy = 100; // (2) pointy = &i[0][2]; // (3) cout << *pointy << endl; cout << *copyPointy << endl; 

After (1) runs, pointy and copyPointy's values are the same: both are the same address, namely, the address of the second element of the second array (where 5 is).

After (2) executes, the element at that address gets changed to 100. pointy and copyPointy both still have the same address, and so they are pointing to the same thing, which has now become the integer 100.

After (3) executes, the value of pointy is now something else - namely, the address of the 3rd element of the 1st array (where 3 is). The value of copyPointy hasn't changed - it wasn't assigned to anything else - so it's still pointing to the same element it was before, namely 100.

Thus when you print the content of the values at their respective addresses, you would expect 3 and 100, which is what you get.

Sign up to request clarification or add additional context in comments.

Comments

2

A pointer is a variable whose value is an address.

Remember this, let's go through your case:

int* pointy = &i[1][1]; 

pointy now holds the address of i[1][1], i.e. pointy -> Address A

int* copyPointy = pointy; 

Now you assign copyPointy the value of pointy, which is the address of i[1][1], e.g. now copyPointy -> Address A and pointy -> Address A.

*pointy = 100; pointy = &i[0][2]; 

These statements assign the content in Address A, 100, and then change pointy to Address B.

It looks like copyPointy -> Address A, which holds 100, and pointy -> Address B. copyPointy does not change with pointy because you only copy the value when assigning.

And also you tag your question c++, you can let copyPointy 3 after you change pointy by using

int * &copyPointy = pointy; 

which means copyPointy is an alias of pointy, once you change the value of pointy the value of copyPointy also changes.

Comments

1

This statement relates to your first question:

int* copyPointy = pointy; 

This means that a copy of the value inside pointy is to be stored inside copyPointy. The value inside pointy was &i[1][1] (the address of i[1][1]). After this statement is executed, inside copyPointy will be &i[1][1].

If we were to do *copyPointy right now, we would have i[1][1] which is 5.

Now look at the following statement int the code:

*pointy = 100; 

Here *pointy = 100; means that the integer value 100 shall replace the value at the address inside pointy. After this statement is executed, i[1][1] will be 100.

The address inside copyPointyhas not changed, it is still &i[1][1]. But the value in i[1][1] has changed from 5 to 100. So when we do *copyPointy we get i[1][1] which is now 100.

Finally, look at this statement:

pointy = &i[0][2]; 

When we execute this statement, pointy will have inside it a new address, namely the address of i[0][2] which contains the integer 3. But the address of copyPointy is still &i[1][1] since it merely took a copy of the previous address in pointy.

As a result, when we do *pointy and *copyPointy at the end, we get the values at two different addresses. Respectively, the integer in i[0][2] and the integer in i[1][1].

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.