APS105 L14 - Pointer Arithmetic
Array Representation in Memory in C
Arrays are stored at a random, unused address by C.
- The array type is just a pointer to the first index of the array.
- Each element in the array is stored in an adjacent cell—this is known as contiguous.
- Thus, the 1st index is stored at an address that is 1 multiple of the size of the array type greater than the 0th index.
- E.g., for an integer array, each element would be stored at the
, .
- E.g., for an integer array, each element would be stored at the
- And since arrays are just a pointer, we can use pointer arithmetic to get the values at different indices of an array.
- Note that we can only add or subtract pointers; multiplying or dividing is not useful.

How an int array is stored in memory. The addresses are adjacent, spaced by 4 cells.
Arrays cannot be treated as pass by values in functions
Since an array is essentially a pointer to where the array is stored, arrays cannot be treated as pass by values; instead, a copy of the address of the array is fed into the function.
- When this happens, the type
int []gets converted toint *, commonly known as array decay.- As a result, no matter the number of elements in the array fed into the function, calling
sizeof(array)will always result in a size of8(on a 64-bit machine) since that is the byte size of the address of the first index of the array. - This is because
sizeofis performed on the type pointed to, so it by default determines the size of the value with typeint *instead ofint [].
- As a result, no matter the number of elements in the array fed into the function, calling
- Thus, any changes to the array within the function affect the function globally.
Obtaining the index of an element in an array
Since arrays are contiguous (each element is stored in adjacent cells), if we know the address of the first element, we can also find the address of an
- It follows that the address should be
startingAddress + i * sizeof(int)for anintarray. - However, C automatically computes
sizeof()for pointer arithmetic.- This ensures that you're not gonna get, for example, the
cell of an intvalue, which would not be useful to you at all. - Thus, the address of an
element is simply startingAddress + i.
- This ensures that you're not gonna get, for example, the
C automatically computes sizeof() when dealing with pointer arithmetic, so adjacent elements in an array have address of startingAddress + i.
In the real world, you should almost always prefer to use array syntax (arr[i]) to get the value of an element, but on a test you might be forced to use pointer arithmetic to get it. Oh well.
Initializing a Pointer to a Safe Address
If you don't know the value of an int yet, you can:
- set the value equal to 0, or:
- set the pointer to
NULLby usingint *p = NULL.
Otherwise, the value at the address that the pointer points to will be "random".
NULL is a special address that is always invalid when dereferenced.
If you try to dereference a pointer that is set to NULL, you'll get a segmentation fault.