need an explanation

i can't understand this code please (i'm a beginner)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  #include<iostream>
using namespace std;
int main()
{
int numbers[5];
int *p;
p=numbers; *p=10
p++; *p=20;
p=&numbers[2]; *p=30;
p=numbers+3; *=40;
p=numbers; *(p+4)=50;
for(n=0;n<5;n++)
cout<<numbers[n]<<", ";
return 0;
}
Lines:

5) Declare an int array with 5 spaces
-Note: an array is similar to a pointer and can be used as one
--The name of the array, or numbers in your case, is also the pointer to the first element
6) Declare a pointer that points to an int
7) Assign the location of the first value in numbers to p; then access that location and assign 10 to it
8) Have p point to the next location, or &(numbers[1]); then access that location to assign 20 to it
9) Have p point to the location of the third element in numbers; then assign 30 to that location
-Note: p = &numbers[2]; is equivalent to p = (numbers + 2); and, specifically on this line, p++;
10) Now have p point to the location of numbers[3]; missing "p" in "*=40" I am presuming
11) Have p again point to the first element of numbers; however, access the location offset by 4 (so numbers[4]) and assign 50 to that.
-Note: *(p+4) is equivalent to *(numbers+4) and numbers[4]
12-13) Print the new contents of the numbers array, which should turn out to be: 10, 20, 30, 40, 50.

Pointer arithmetic.
Thx alot
Topic archived. No new replies allowed.