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 floatsThe * 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.
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) operatorLet'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.
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.
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);
}
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