Monday, 30 April 2012

Pointer Arithmetic and Arrays in C++


#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
      int  n[8] = {3, 4, 5, 6, 7, 8, 9, 10};
      int* nptr;
      int  i;

      cout << "The array using n[i] notation" << endl;

      for (i = 0; i < 5; ++i)
        cout << setw(4) << n[i];

      cout << endl << endl;

      cout << "The array using *(n + i) notation" << endl;

      for (i = 0; i < 5 ; ++i)
        cout << setw(4) << *(n + i);

      cout << endl << endl;

      cout << "The array using nptr" << endl;

      for (nptr = n; nptr < n + 5; ++nptr)
        cout << setw(4) << *nptr;

      cout << endl << endl;

      return 0;
}

No comments:

Post a Comment