// more pointers#include <iostream>usingnamespace std;
int main ()
{
int numbers[5]; // Declare an array of 5 integersint * 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, 50return 0;
}