× Home (CS 211) Notes Projects Waxman's Website

General

A pointer stores the address of another variable.
The & operator give the address of a vairable. For example, &x is the address of x
The * operator is different from multipliation of two vairables.
In terms of pointers, * is a dereference operator. *x is the value that x is pointing to.
* is also used to declare a pointer

pointer-code

Arrays as Pointers

An array is a fixed-size collection of elements of the same type.
Arrays are stored in continuous memory.
array1
Array names are not pointers, though they can be treated like pointers in certain cases. We can deference a pointer to access whatever it is pointing to.
array2
a: by it self, is pointing to the beginning of the array. The array starts at index 0 so the value of *a is a[0] or 3.
a+0: pointing to the beginning of the array with an offset of 0. Hence it's still pointing at the first index, 3.
a+1: pointing to the beginning of the array with an offset of 1. But one of what? Since this array is an array of integers, the offset is the size of an integer. So a+1 is one integer away from the first index and since arrays are stored in continuous memory, a+1 is pointing to the second index, a[1] or 1.
a+2: pointing to the beginning of the array with an offset of 2 integers. This is pointing to the third index of the array, a[2] or 4.

Mutli-dimenstional arrays

A 2D array is a 1D array of 1D arrays. You can visualize it as a table, but the elements are stored continuously, starting with the 1st row and goes row by row.
array3
sizof(arrayName) gives you the size of the entire array in bytes.
sizeof(pointer) gives you the size of a pointer.

arraypointer-code

Related: Hexadecimal