Pointer and Array

Hi, I need some explanations regarding this code below. I do not know how to get the "cout" because I am totally blurred with the "*" and "&". Could anyone help me?
Thanks! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;


int main(){
    int a[7] = {7,9,5,2,3,6,4};
    int *ptr=&a[1]; //ele 2
    *ptr=5;
    cout<<ptr[0]<<endl;   // 5 - ele 2
    cout<<*(ptr+3)<<endl; // 3 – 3 ele after ele 2
    cout<<*ptr-1<<endl;   // 4 – (ele 2 value – 1)
    return 0;
}
The int * ptr; reads: name "ptr" is a pointer variable that will point to type int objects.

Pointer arithmetic and dereferencing:
1
2
3
4
5
6
7
8
9
10
11
12
int k;
int * ptr;

*ptr // dereference pointer, aka access value at the memory location that the ptr points to
// same as
*(ptr+0)
// same as
ptr[0]

ptr[k]
// is same as
*(ptr + k)
Thanks! :)
Now I have an idea what the code is about!
Topic archived. No new replies allowed.