• Pointer Arithmetic

    What is pointer arithmetic?:

    • operations performed on pointers:
      • pointer = &variable: a pointer variable can be assigned the address of an ordinary variable
      • pointer1 = 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 type
      • pointer = NULL: a pointer can be assigned a null (zero) value to indicate a special condition, e.g. the pointer variable has not been assigned
      • pointer + 3, ++pointer: an integer quantity can be added to or subtracted from a pointer variable
      • pointer1 - 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 to
      • pointer1 >= pointer2: two pointer variables can be compared if both pointers point to elements of the same data type
        • pointer1 == pointer2: is pointer1 and pointer2 pointing to the same object
        • pointer1 >= pointer2: is pointer1 pointing to an array element that has a higher index than the array element that pointer2 is pointing to
          • a[1] > a[0] is true
          • a[1] < a[0] is false

    Why are pointer arithmetic important?:

  • Using Pointers vs Array Index

    Example:

    pointer_arithmetic_vs_array_index.c:

      Things to note:
    • you need to dereference after the pointer arithmetic, i.e. *(array_pointer + 5) happens after *(array_pointer + 5)
    • both ways are equivalent
      • recommend using array indexing
      • use pointers only if absolutely necessary, e.g. when dealing with complex data structures
  • Other Considerations

    Arrays are actually just pointers

    Pointer arithmetic and array indexing takes care of the byte counting for you

  • Showing Array Backwards with Pointer Arithmetic

    Update backward_array.c so the program shows the array elements backwards

  • Assignment

    No graded assignments
  • Optional Assignments

    Optional assignments are not graded and does not need to be turned in. It is intended to give you more practice (if you want it) using the programming concepts discussed in lecture. Some optional assignments are intended to introduce related concepts (if you care about it) not discussed in this course to broaden your knowledge about programming in general.

    If the optional assignment is modifying a required assignment, be sure to leave your previous assignment work untouched by copying them to a new folder for the optional assignment and modify only the files in the new folder

    Print out a 2D array with pointers

    Here is the relationship between pointers and array indexing that I was trying to bring to light:

    Check for palindromes:

    • Write a program that allows the user to enter in a phrase/word. The program then checks if the phrase/word is a palindrome or not.
    • A palindrome is a word/phrase that is spelled the same forward and backwards: http://en.wikipedia.org/wiki/Palindrome