CSCI 1300
Lecture Notes


11/5/97

Administrative Stuff 



Administrative Stuff

  • Office hours (one-time change)
  • No office hours today
  • Office hours tomorrow 9-12
  • Lab hours (one-time change)
  • No Lab hours on Monday
  • Wednesday 2-7.
  • Pointers

  • Pointer variables

  • Pointer parameters

  • Pointer arithmetic

  • Pointers to derived objects

  • Pointers and arrays

  • Pointers to pointers

  • Dynamic memory allocation and deallocation



    Pointer variables

    A pointer variable
  • Is a variable that contains the address in memory of an object.
  • Has a type, that matches the type of the objects that it can point to.
  • Can be made to point to any object of the appropriate type.
  • Can be used to access the object that it points to.
  • Is indicated by the *operator in the declaration.
  • Examples:
  • int *foo;
  • double *distance;
  • char *cp;
  • int *list[10];
  • The & operator
  • When applied to an object, returns the address in memory of that object.
  • Can be used to initialize a pointer variable.
  • Examples:
      int i = 5;
      double radius = 3.7;
      char c;
    
      foo = &i;
    
      distance = &radius;
    
      cp = &c;
    
    
    The * operator, when applied to a pointer variable, accesses the variable that the pointer points to.
    So, what do we get if we execute:
  • cout << *foo;
  • cout << *distance;
  • cout << *cp;

  • Pointer variables

    Another way to accomplish the same thing as reference parameters is to use pointers.