Skip over navigation

Arrays: Memory

Arrays and Pointers

Terms List

Problems and Solutions 1

If you are not already familiar with pointers, you should review the Spark Note on memory and pointers.

All of this time we have been using our array variable with the syntax [<index>] to index into the array. We, will now discuss the actual value stored in the array variable. You may have guessed that an array variable holds an entire array the same way that an integer value holds an entire integer. This is not the case. Instead, the array value itself is just a pointer to the memory address where the array begins, as illustrated in the following image:

Figure %: Pointer to Array

This is the reason that you cannot simply assign the value of one array into another array and expect it to create a new copy. Instead, if you have two array variables, and you assign one to the other it will simple mean that you are assigning the address where the first array starts into the second array so that they both point to the same chunk of memory.

Figure %: Array Assignment

If you were planning to use a variable to store only an address, then you would not necessarily want to allocate a chunk of memory when you declared it. To achieve such a variable you could either use the syntax for declaring a pointer:


int *arr_p;

Or you could declare it as you would a normal array but just leave the square brackets empty.


int arr_p[];

In the next section we will discuss more of the implications of arrays being pointers.

Follow Us