I will try to explain.
(You need to understand the concept of pointers first:
http://www.cplusplus.com/doc/tutorial/pointers.html)
An array is a set of constant pointers. At the moment you declare an array like this:
int array[3]
, the program will reserve place in the memory to store three integer values. You can acces those values by using
array[n]
, where n is 0/1/2. This is in fact a constant pointer to that specific place in memory.
For so far the normal part. Now our problem: wy can you use an array[index] wich we havent declared? Assuming an array is a constant pointer, a declaration as
1 2
|
int array[3]={1,2,3};
array[10]=10;
|
will reserve 3 places in the memory and store the values 1,2 and 3 in them, and store the value 10 in the place arrayj[10] is pointing at. This place is
not reserved. In the case of a simple program like you got above, this placed isnt used to store another value in before using it. So when you use this:
cout<<array[10]
the program displays 10 on the screen: you have stored that value a few lines before in this specific place.
However, in a bigger program, you probably
will use this non-reserved place in memory, so the program will start acting strange.
I hoop you understand it now.