Arrays in C++
As a computer programming hobbyist (who is occasionally called upon to write the odd small commercial
application) Bot has started playing around with C++ again recently. Partly because Bot wants to do more
with OpenGL and assembly (maybe more on these in later articles) and partly because it's fun!

Here I include a small code snippet on arrays in C++ (which I have tested in CodeBlocks). There are many
misconceptions about arrays in C++, even amongst experienced programmers. This is not critical, as there
is more than one way to get the job done. However, not everyone realises that when passing an array to a
function in C++ it is not necessary to use an explicit pointer to that array, because the array name behaves
like a pointer by default and arrays are not copied when passed to functions, but the array name is sent as a
kind of pointer. (I did come across a minor technical difference between the array name and conventional
pointers in someone's blog debate, which I have forgotten, but the array name behaves like a pointer to the
array). The following code demonstrates this:

    #include <iostream>

    using namespace std;

    void modifyArray(int inArray[], int numElements);
    void displayArray(int myArrat[], int numElements);

    int main()
    {
       cout << "Array test!" << endl;
       int arrayOfInts[8] = { 2, 4, 6, 8, 10, 0, 1, 5 };
       int numberOfElements = sizeof(arrayOfInts)/sizeof(int);
       cout << "The size of the array in memory = " << sizeof(arrayOfInts) << endl;
       int* arrayPtr = arrayOfInts;
       cout << "Size of a pointer to this array = " << sizeof(arrayPtr) << endl; // = 4
       cout << "The size of what the pointer points to (an int) = " << sizeof(*arrayPtr) << endl;
       cout << "The pointer = " << arrayPtr << endl;

       /*
       //Just a little test to illustrate a point about pointers!
       cout << endl;
       short myShort = 8;
       short* shortPtr = &myShort;
       cout << "Size of a ptr to a short = " << sizeof(shortPtr) << endl;
       cout << "The size of what the short ptr points to = " << sizeof(*shortPtr) << endl;
       cout << endl;
       */

       cout << "Number of elements in array = " << numberOfElements << endl;

       displayArray(arrayOfInts, numberOfElements);
       modifyArray(arrayOfInts, numberOfElements);
       displayArray(arrayOfInts, numberOfElements);

       cout << "Press return to continue" << endl;
       string userInput = "";
       getline(cin, userInput);

       return 0;
    }

    void modifyArray(int inArray[], int numElements)
    {
       for(int i = 0; i < numElements; i++)
       {
           inArray[i] = 1;
       }
    }

    void displayArray(int myArray[], int numElements)
    {
       cout << "The following has been passed to this function: " << myArray << endl;
       cout << "The size of the array passed = " << sizeof(myArray) << endl;
       cout << "The elements in the array are: " << endl;
       for(int i = 0; i < numElements; i++)
       {
           //Note either of the following methods work!
           //cout << myArray[i] << endl;
           cout << *(myArray + i) << endl;
       }
    }
Notice that the array name, rather than an explicitly declared pointer to the array, is passed to the
functions that handle the array. Furthermore, the modifyArray() function successfully changes the array
element values, so no temporary copy of the array was passed to the function; that is, it has not been
passed by value in the usual sense. If a copy of the array would been passed, then this would lead to the
well-known problem of changing the copy only, so when the array is displayed again after called the
modifyArray() function there would be no changes to the original array.

What is passed is the address of the first element of the array, that is a pointer to the array. Indeed the
address is exactly the same as the value of a pointer created explicitly to point to the array, since this
value is the memory address of the first element of the array. The value of the address of the first
element of the array is actually what is passed to the function. Essentially a reference of sorts, to the
array, has been passed to the function. We can then use this array name much like a pointer or as an
array, retrieving values either using array notation: e.g. myArray[2] retrieves the third element (we start
counting at zero) or using pointer style syntax: *(myArray + 2) returns the value of (dereferences) the
third element of the array. Note that myArray + 2 would simply return the address of the third element!

Notice also that the memory size of the array name received by the function is the same size as the
pointer to the array (i.e. 4 bytes, the size of a memory address). The output on my PC is shown below:
Playing around and testing (what is rather trivial) code in this way is a good way to learn programming,
and is also great fun!


Article last updated: 18/1/2014
screenshot of output