Pointers


A pointer is an address. We can declare pointer variables like this:

  int *ptrInt;     // ptrInt is a pointer to an integer
  char *ptrCh;     // ptrCh is a pointer to a character
  float *f, *g;    // f and g are pointers to floats
The * is used to indicate that the variable is a pointer. Note that pointers are pointers to particular types. You must declare a pointer variable as a pointer to an int, or a pointer to a float, etc.
We assign addresses to pointer variables this way:
  int *ptrInt;
  int num=5;

  ptrInt = #
The & is the address-of operator. So the above assignment statement puts the address of num into the pointer variable ptrInt.

(The following code was traced through in class)

  int *valuePtr;
  int value, num;
  
  char *p1, *p2;
  char ch;
  .
  .
  .
  value = 24;
  valuePtr = &value;
  p1 = &ch;
  p2 = p1;
  num = *valuePtr;    // indirection (or dereferencing) operator
Let's look at the last statement. num = *valuePtr; When the * is used in an expression with a pointer variable, it dereferences the pointer. so *valuePtr returns the value stored at the location pointed to by valuePtr.

Pointers and arrays

In C++, pointers and arrays are tightly connected. Remember how we pass an array as a parameter in a function call:

  NegateArray(arrayToNegate, 100);
We said that when you use the name of an array all by itself in an expression (without any subscripts), then its value is the address of the 0th element of the array. Well, a pointer is an address, too. So I can make the following assignment:
  int *p;                  // p is a pointer to an int
  int arr[100];

  p = arr;                 // p gets the address of arr[0]
Notice that there is no & used here. p=arr has exactly the same effect as p = &arr[0];. Both of these statements store the address of the base of the array into p. We now have two ways to access the array, one using subscript notation and one using pointer notation:
// put 0's in every location of arr

   for (i=0; i<100; i++)
     arr[i] = 0;

// do it again, using pointers

   for (i=0; i<100; i++){
     *p = 0;
     p++;
   }

// or we can use subscript notation with p

   for (i=0; i<100; i++)
     p[i] = 0;
So the thing to remember is that an array name is a pointer. Our prototype for Negate array could be either
void NegateArray(int arrayToNegate[], int numelements);
or
void NegateArray(int *arrayToNegate, int numelements);
Whether we declare the formal parameter as int arrayToNegate[] or int *arrayToNegate, the result is exactly the same to the C++ compiler; within the function, arrayToNegate is simply a variable that points to the beginning of the caller's actual array.
Why are we learning this?
  1. C programmers use pointers all the time, because there are some things in C that you just can't do without using pointers (passing arguments by reference). As C++ programmers, many of you will have to look at/debug code written by C programmers. If that code uses pointers, you'll need to know what's going on.
  2. In CS2005 you'll use dynamically-allocated memory to build data structures. You have to understand what pointers are in order to use memory this way.

(CS 2005, you can skip the rest of this handout!)

Pointers and strings

We've just seen that pointers=arrays. We know that strings are arrays. So do
pointers=strings? Yes!
We can declare a string this way:

char *name="Glynis";
This sets aside a memory location for name, which holds the address of the string "Glynis". It would be OK to use an assignment statement with this method of declaring strings:

  char *name="Glynis";

  name = "Hamel";
Let's look at a program that uses three different methods to determine the length of a string

#include<iostream>

using namespace std;

int main()
{
  char name[] = "Santa";
  char title[] = "top elf";
  char phrase[] = "Ho, ho, ho!";

  // the prototypes for the three length functions

  int length1(const char string[]);
  int length2(const char *string);
  int length3(const char *string);

  cout << name << " has length " << length1(name) << endl;
  cout << title << " has length " << length2(title) << endl;
  cout << phrase << " has length " << length3(phrase) << endl;
  return 0;
}

int length1 (const char string[])
{
  int count = 0;
  while (string[count] != '\0')
    count++;
  return (count);
}

int length2 (const char *string)
{
  int count = 0;
  while (*string)
    {
      count++;
      string++;
    }
  return (count);
}

int length3 (const char *string)
{
  int count = 0;
  while (*string++)
    count++;
  return (count);
}

About this document ...

This document was generated using the LaTeX2HTML translator Version 96.1 (Feb 5, 1996) Copyright © 1993, 1994, 1995, 1996, Nikos Drakos, Computer Based Learning Unit, University of Leeds.

The command line arguments were:
latex2html -split 0 pointers.tex.

The translation was initiated by Glynis Hamel on Fri Jul 9 15:22:30 EDT 1999


Glynis Hamel
Fri Jul 9 15:22:30 EDT 1999