>> understanding behind arrays
Without knowing what you know already this may be difficult, but here's a general presentation.
Arrays are variables that can hold the information about a group of objects in contiguous memory. You can use an array almost like any other object. For example you can refer to it by it's name. You can pass it to functions. If the type of the array includes the keyword const, then the information in the array cannot be changed after initialization. Arrays can be declared using static or dynamic memory (if you don't know about the difference between static and dynamic memory yet, you will learn about later). And so on. One big difference to other categories of variables, however, is that you cannot assign one array to another.
Each ojbect that has information stored in the array must be of the same type. The information about a given object stored in the array is frequently callled an element of the array.
The type of objects that can have information stored in the array must be known at compile time and, if the array is going to be declared using static memory, which is what you'll be doing now, then the maximum number of objects that could be stored in the array must be known at compile time, too. So,
int a1[10];
is the declaration of an array of 10 objects of type int and
const int NUM = 8;
Car a2[NUM];
a2 is an array of 8 Cars. Note than both 10 and NUM are of type const int. That is a requirement for declaring arrays using static memory.
You can access the information about each object in an array by using the [] operator. The sytnax is like this:
a1[x];
x is frequently called the index of the object (information) and it must resolve to an int. x ranges from zero to one less than the maximum number of objects in a1. Using x with the value of maximum number of ojects in a1 or beyond is a frequent source of errors. So for example, in a1 above the index could be from 0-9 and in a2 the index coud be 0-7. To extend this further the first element in a2 can be accessed using the following notation:
a2[0];
and the third element in a2 can be accessed using the following notation
a2[2];
If elements in the array aren't intialized or assigned a value before you try to use them, then they will be of undetermined value, that is junk. Each element of the array may be initialized to the same value using an initialization statement like this:
int a1[10] = {0};
This means that all 10 elements of a1 will be initialized with the value of zero. Alternatively, each element in an array may be initialized to a different value:
int a1[4] = {99, 5, -27, 555};
means that the a1[0] is initialized to 99, a1[1] is initialized to 5, a1[2] is initialized to -27, a1[2] is initialized to 555.
In addition, as long as the type of the array isn't constant, each element in the array can be assigned any valid value desired.
for(int i; i < 10; ++i)
a1[i] = i;
means that each element of a1 will be assigned the value of it's index.
Each element in the array can be used just like an object of that type. So this:
int a1[3] = {0};
int num = a1[1];
cout << num;
will print the value of 0 to the screen.
The name of the array can be used as the address of the first element in the array. Thus
int a1[10] = {4};
int * num = &a1[0];
int * num1 = a1;
cout << *num << " + " << *num1 << endl;
will print 4 + 4 to the screen.
Arrays can be passed to functions. Thus, if you have a function whose prototype looks like this:
void func(int []);
then you could pass it an array of type int like this:
func(a1);
One confusing tidbit is that this will also work:
void func2(int *)
func2(a1);
as long as a1 is the name of an array of type int.
Arrays are passed to functions by reference automatically. So if you don't want the function to change any information in the array it is passed you should make it a const type.
Because an array name can be used like a pointer in many circumstances some people mistakenly believe it is a pointer, but it's not.
You should also know that in C++ there is a special type of an array called a null terminated char array and there are special rules for handling null terminated char arrays. The importance of null terminated char arrays is that all strings are based on null terminated char arrays. But that's a discussion probably best kept for later, too.
Another topic concerning arrays is that they aren't limited to a single "dimension". That is, an array can be an array of other arrays. But don't worry, you'll learn about multidimensional arrays later, too.