Pointers

The address of a variable

  1. address.C: output the address of a variable in hexadecimal.
    The value of i is 10
    The address of i is 0x7fff2757276c
    The number of bytes in i is 4
    
  2. Store the address of a variable into a pointer.
    Diagram with pointer pointing left.
    1. pointerint.C: store the address of an int into a “pointer to an int”.
    2. pointerdouble.C: store the address of a double into a “pointer to a double”.
  3. Use the unary * operator to dereference the pointer, i.e., get the value to which the pointer points.
    (The binary * operator means multiplication.)
    1. dereferenceint.C
    2. dereferencedouble.C
  4. neighbor.C. A pointer also gives us access to the values that are neighbors of the pointed-to value.
  5. struct.C: use the unary * operator to dereference a “pointer to a structure” to get the value of each field of the original structure.