Safe Array Echo
/* * Read some integers into an array, then echo them, using pointers. */ #include <iostream> using namespace std; const int ARRMAX = 100; main() { /* Read them in, but check so we don't pass the end of the array. */ int arr[ARRMAX]; /* Array of numbers. */ int *scan = arr; cout << "Enter some integers: " << endl; while(scan < arr + ARRMAX && cin >> *scan) ++scan; int *end = scan; /* Print them back out again. We don't bother with a size check since we know we didn't read too many in. */ cout << "===============" << endl; for(scan = arr; scan < end; scan++) cout << *scan << " "; cout << endl; }

Same thing again, this time being carful not to take the array out of bounds.