View Single Post
  #1 (permalink)  
Old June 8th, 2003, 11:18 PM
odie odie is offline
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Pointers and Arrays C++

In this little program I enter 10 integers and display them back
using arithmetical operation to a pointer.
it compiles, accepts my input, display the elements, but right after
that it causes an error. if I delete the "delete [] numbers" I will
be OK and if I use the pointer notation syntax "*(numbers + i)"
it works fine. here's the code...
can anyone explain me why? TIA

#include <iostream>

using namespace std;

int main ()
{
    const int max_value = 10;
    int* numbers = new int[max_value];
    int cnt = 0;

    do
    {
        cout << "\nPlease, enter integer #" << cnt + 1 << ": ";
        cin >> *(numbers + cnt);
        cnt++;
    }while (cnt < 10);

    for (int i = 0; i < max_value; i++, numbers++)
        cout << endl << *numbers << endl;

    delete [] numbers;

    cout << endl;
    return 0;
}