Pointers and arrays

Write your question here.
Hello guys, I'm a beginner in C++ and I have a question. Can someone explain to me the lines 15th, 16th, 17th and 18th from my code . Because I can't understand the result from them why it is how it is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream>
using namespace std;
int main()
{
	double a[] = { 1 , 2, 3 , 4 , 5 ,6, 7 };
	short len = sizeof(a) / sizeof(a[0]);
	for (short i = 0; i < len; i++) cout << "  "<< a[i]<< endl;
	double *u = a;
	double *us[] = { a + 1, &a[3], &a[5], a + 2, &a[4] };
	double **au1 = &u;
	double **au2 = &us[3];
	double **au3 = &us[4];
	cout << "a[2] = " << a[2] << endl;
	cout << "*(u + 2 ) = " << *(u + 2) << endl; // what does this mean ? 
	cout << "**(us + 3) = " << **(us + 3) << endl; // what does this mean ? 
	cout << "(*us)[1] = " << (*us)[1] << endl; // what does this mean ? 
	cout << "*au1 + 2 = " << (*au1 + 2) << endl;/// what does this mean ? 
	cout << "**(au3 - 1) = " << **(au3 - 1)  << endl; // what does this mean ?
	system("pause");
	return 0;
}
Your first line (line 15) will dereference the memory address 2 places after the memory address stored in u. Since u points to the first variable in a (array to pointer conversion rules), this will be the equivalent of writing a[2].

Your next line works nearly the same. First the variable us is automatically converted to a pointer to its first element, then this pointer is incremented by 3 (meaning we are at us[3] now). This is contains the value a+2, which due to the pointer to array conversion rules, is the same as a pointer to a[2]. We dereference this twice, meaning we are now at a[2].

Third, the variable us is defererenced first. Due to array to pointer conversion, the array us will be converted to a pointer to its first element first. We then dereference this pointer, meaning we have the value stored in us[0]. This contains the value of a + 1. Which is the equivalent of a pointer to a[1]. We then subscript this with the value 1, which equals adding 1 to the pointer and dereferencing it. So we end up at a[1+1] is a[2].

The fourth, you first dereference the variable au1, this contains a pointer to the variable u. Dereferencing it will give us the variable u, which is a pointer to the first element of a (pointer to array conversion again). We increment this with 2, meaning we have a pointer to a[2]. I think that your original example probably contained another derefernce, since now you are printing the contents of a pointer to a[2], meaning the address of a[2].

And last but not least, the fifth one. You start by subtracting one from au3, which is a pointer to us[4]. This will give us a pointer to us[3], which contains a pointer to element 2 of a again. So we actually have a pointer to a pointer to a[2] this time. We dereference this twice, giving us a[2] again.

So except for line 17, all of these lines do the exact same, printing a[2] (which contains the value 3). Line 17 prints the address of a[2], and I suspect you originally intended to dereference this once more to get a[2] again, like so: (*(*au + 2)).

Nearly all of these examples depend on array to pointer conversion. This basically means that an array is converted to a pointer to its first element.
Thank you very much ! You've helped me a lot ! :) And about line 17 , yes you were right about that also. Thanks again.
Last edited on
Topic archived. No new replies allowed.