Array / Pointers query

Hi,
I'm trying to understand an aspect of the code below, and can't figure it out. I've added a couple of extra cout's to the code to try and understand it, but the original code is from an online course.
So, the output from the code below is:

*p1 = 2
*p2 = 4
*p1 = 1
t[p1 - p2] = 2
-1

I can figure out everything except why t[p1 -p2] is equal to 2. I just don't see how it can get to being 2, but I guess that's because I'm completely misunderstanding something.

Any assistance appreciated! Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main(void) {
        int t[4] = {8, 4, 2, 1};
        int *p1 = t+2, *p2 = p1-1;

        cout << "*p1 = " << *p1 << endl;
        cout << "*p2 = " << *p2 << endl;

        p1++;

        cout << "*p1 = " << *p1 << endl;
        cout << "t[p1 - p2] = " << t[p1 - p2] << endl;
        cout << *p1 - t[p1 - p2] << endl;

return 0;
}
Last edited on
Does this help?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int t[4] = {8, 4, 2, 1};

int *p1 = t+2;
// let p1 point to the element at offset 2 from the beginning of t


int *p2 = p1-1;
// let p2 point to the object immediately before *p1
// p2 points to the element at offset 1 from the beginning of t;
// p2 = t + 1

cout << "*p1 = " << *p1 << endl;
// *p1 is 2 - the third element in the array t

cout << "*p2 = " << *p2 << endl;
// *p2 is 4 - the second element in the array t 


p1++;
// let p1 point to the object immediately after *p1
// p1 points to the element at offset 3 from the beginning of t
// p1 = t + 3

cout << "*p1 = " << *p1 << endl;
// *p1 is 1

cout << "t[p1 - p2] = " << t[p1 - p2] << endl;
cout << *p1 - t[p1 - p2] << endl;
// p1 - p2 = (t + 3) - (t + 1) = 2  
Last edited on
Thank you for your reply and code comments, but I still don't understand that last line of code:

 
cout << *p1 - t[p1 - p2]

I get everything else, but that line confuses me entirely. I just don't see it at all.

To my untrained eye, it looks like it's saying: "okay, take the value of the p1 pointer (which we know is 1) and subtract from it some value(s) of the array". Then I'm getting confused trying to understand what t[p1 - p2] actually does. I mean, the value of t[p1] appears to be 1, while the value of t[p2] appears to be 4. But that's obviously not right, since 1 - 4 is -3, and the actual result is 2.

Confused. The whole thing with pointers and arrays (and C++!) is new to me, but hopefully I can learn.
Last edited on
okay, take the value of the p1 pointer (which we know is 1)

No, the value of p1 is t + 3! The value of *p1 is 1: p1 points to the final element in the array. Similarly, the value of p2 is t + 1. The value of *p2 is 4, it points to the second element in the array.

When we say p1 - p2, we're subtracting the values of the pointers to give (t + 3) - (t + 1) = 2, and t[2] is just 2.

The whole thing with pointers and arrays (and C++!) is new to me, but hopefully I can learn.

Oh, you'll get it. This topic is famously confusing until it clicks.
Last edited on
Oooh. I see. Yeah, I get what you mean now. Still, I don't think it will come naturally for a while yet. Hopefully I can get some good practice and get familiar with the ideas.

Thank you very much!
Topic archived. No new replies allowed.