Pointers and Arrays

Can someone comment out line by line the below code?
I can't understand what is happening.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  // more pointers
  #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; *p = 40;
  p = numbers; *(p+4) = 50;
 
 for (int n=0; n<5; n++)
   cout << numbers[n] << ", ";
 return 0;
}
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
30
31
32
33
34
35
36
#include <iostream>

int main ()
{
    constexpr int N = 5 ;
    int numbers[N] = {0} ;

    std::cout << N << " elements of array numbers are located at:\n\t" ;
    for( int i=0 ; i<N ; ++i ) std::cout << &numbers[i] << ' ' ;
    std::cout << '\n' ;

    int* p = nullptr ;
    p = numbers ;
    std::cout << "after p = numbers, pointer p points to address: " << p << '\n' ;
    *p = 10;

    ++p ;
    std::cout << "after ++p, p points to address: " << p << '\n' ;
    *p = 20;

    p = &numbers[2];
    std::cout << "after p = &numbers[2], p points to address: " << p << '\n' ;
    *p = 30;

    p = numbers + 3;
    std::cout << "after p = numbers+3, p points to address: " << p << '\n' ;
    *p = 40;

    p = numbers;
    std::cout << "after p = numbers, p points to address: " << p << '\n' ;
    std::cout << "(p+4) points to address: " << p+4 << '\n' ;
    *(p+4) = 50;

    for( int v : numbers ) std::cout << v << ' ' ;
    std::cout << '\n' ;
}

http://ideone.com/auVwyU
Since you put multiple statements on the same line, I broke them up:

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
30
31
32
33
34
35
36
  // more pointers
  #include <iostream>
  using namespace std;
 
int main ()
{
  int numbers[5];           // Declare an array of 5 integers
  int * p;                  // Declare a pointer
  p = numbers;              // makes our pointer 'p' point to the first entry in the 'numbers' array.
                            //  that is.. it points to numbers[0].
                            //  using an array name without [brackets] like this is shorthand for a pointer
                            //  to the first element.  This statement is the same as:
                            //    p = &numbers[0]
  *p = 10;                  // assigns 10 to whatever p points to.  Since p points to numbers[0],
                            //   this effectively assigns numbers[0].  IE:  same as doing:
                            //      numbers[0] = 10;
  p++;                      // This increments the pointer so it points to the next integer in memory
                            //   which would be the next element in the array.  So now, p points to
                            //   numbers[1]
  *p = 20;                  // Assigns 20 to whatever p points to (numbers[1])
  p = &numbers[2];          // Makes p point to numbers[2]
  *p = 30;                  // Assigns 30 to whatever p points to (numbers[2])
  p = numbers + 3;          // Takes a pointer to the first element in the array (numbers[0] .. same shorthand
                            //   we saw above), and adds 3 elements to it to get a pointer to the [3] element.
                            //   then assigns that to p.  So now p points to numbers[3]
  *p = 40;                  // Assigns 40 to whatever p points to (numbers[3])
  p = numbers;              // Resets p so it points back to numbers[0]
  *(p+4) = 50;              // This takes the pointer stored in p (&numbers[0]), adds 4 elements to it
                            //   (giving us &numbers[4]), then assigns 50 to whatever that result points to
                            //   since that result points to numbers[4], this assigns 50 to numbers[4]
 
 for (int n=0; n<5; n++)
   cout << numbers[n] << ", ";  // prints all entries of numbers, which should result in:
                                //  10, 20, 30, 40, 50
 return 0;
}



EDIT:

ninja'd!
Last edited on
Thanks ninja'd! And what's the below?
Express it in simple English.


1
2
3
4

a[5] = 0; // a [offset of 5] = 0
*(a+5) = 0; // pointed by (a+5) = 0
1
2
a[5] = 0; // a [offset of 5] = 0
*(a+5) = 0; // pointed by (a+5) = 0 


Both mean the same thing ;
1
2
3
4
5
6
7
8
9
10
11
12
int a[5];
int *b = new int[5]; 
//Both are the same except variable a will be automatically deallocated whereas b needs to manually deallocated
a[0] = 1;
a[1] = 2;
//or can also be done as
//*(a+0) = 1; // points to *a
//*(a+1) = 2; // points to the next block of 4 bytes after *a;
//Oh and not to forget this
std::cout << "a[0] = " << *(a+0) << std::endl; //Prints 1;
std::cout << "a[1] = " << *(a+1) << std::endl; //Prints 2;
delete b;
Last edited on
delete [] b;
Have a go at reading chapter 5 in this book - it may help you a bit.

http://zanasi.chem.unisa.it/download/C.pdf
Topic archived. No new replies allowed.