What is pointer arithmetic?:
- operations performed on pointers:
pointer = &variable
: a pointer variable can be assigned the address of an ordinary variablepointer1 = pointer2
: a pointer variable can be assigned the value of another pointer variable (which is usually an address), if both pointers point to objects of the same data typepointer = NULL
: a pointer can be assigned a null (zero) value to indicate a special condition, e.g. the pointer variable has not been assignedpointer + 3
,++pointer
: an integer quantity can be added to or subtracted from a pointer variablepointer1 - pointer2
: one pointer variable can be subtracted from another pointer variable if both pointers point to elements of the same array, e.g. find the number of array elements between the array elements the pointers are pointing topointer1 >= pointer2
: two pointer variables can be compared if both pointers point to elements of the same data typepointer1 == pointer2
: is pointer1 and pointer2 pointing to the same objectpointer1 >= pointer2
: is pointer1 pointing to an array element that has a higher index than the array element that pointer2 is pointing toa[1] > a[0]
is truea[1] < a[0]
is false
Why are pointer arithmetic important?:
- allows you to "walk" around complex structures
- you can have arrays of arrays
- you can have arrays of pointers
- you can have pointers to pointer to array
- allows you to compare the index of array elements
- examples of data structures