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.
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.