Arrays And Pointers
#include <iostream> #include <iomanip> using namespace std; const int ARRSIZE = 7; /* * Print and array of size ARRSIZE showing subscripts. */ void pr_array(int arr[ARRSIZE]) { // Print the subscripts. Printing the setw(4) doesn't // print anything, but specifies that the next number // should be printed with a minimum size of four. int m; for(m = 0; m < 7; m++) cout << setw(4) << m; cout << endl; // Print the array values, min size four. for(m = 0; m < 7; m++) cout << setw(4) << arr[m]; cout << endl; } int main() { int fred[ARRSIZE] = { 10, 20, 30, 40, 50, 60, 70 }; cout << "Original:" << endl; pr_array(fred); int *joe = fred + 5; // Points to fred[5] int *alex = joe - 3; // Points to fred[5-3] *fred = 99; *joe = 199; *alex = 299; fred[1] = 399; alex[4] = 599; alex += 2; // Points to fred[4] now. *alex = 699; cout << "\nModifed:" << endl; pr_array(fred); }

It is possible to do arithmetic on pointers, which gives the same result as arithmetic on the subscript it points to.