Ch10 - Pointers Revision
Ch10 - Pointers Revision
#include <iostream.h>
int main()
{
int var1, var2; //two integer variables
int* ptr; //pointer to integers
ptr = &var1; //set pointer to address of var1
*ptr = 37; //same as var1=37
var2 = *ptr; //same as var2=var1
cout << var2 << endl; //verify var2 is 37
return 0;
}
Using the indirection operator to access the value stored in an
address is called indirect addressing, or sometimes
dereferencing, the pointer
The address that is put in a pointer must be the
same type as the pointer.
One can’t assign the address of a float variable to a
pointer to int, for example:
float flovar = 98.6;
int* ptrint = &flovar; //ERROR: can’t assign float*
to int*
There is a sort of general-purpose pointer that can
point to any data type. This is called a pointer to
void, and is defined like this:
void* ptr; //ptr can point to any data
type
Such pointers have certain specialized uses, such
as passing pointers to functions that operate
independently of the data type pointed to.
// pointers to type void
#include <iostream.h>
int main()
{
int intvar; //integer variable
float flovar; //float variable
int* ptrint; //define pointer to int
float* ptrflo; //define pointer to float
void* ptrvoid; //define pointer to void
ptrint = &intvar; //ok, int* to int*
// ptrint = &flovar; //error, float* to int*
// ptrflo = &intvar; //error, int* to float*
ptrflo = &flovar; //ok, float* to float*
ptrvoid = &intvar; //ok, int* to void*
ptrvoid = &flovar; //ok, float* to void*
return 0;
}
// ptrnote.cpp
// array accessed with pointer notation
#include <iostream.h>
int main()
{ //array
int intarray[5] = { 31, 54, 77, 52, 93 };
for(int j=0; j<5; j++) //for each
element,
cout << *(intarray+j) << endl; //print value
return 0;
}
The expression *(intarray+j) in PTRNOTE has exactly the
same effect as intarray[j]
But how do we interpret the expression *(intarray+j)?
◦ Suppose j is 3, so the expression is equivalent to *(intarray+3).
◦ Represent the contents of the fourth element of the array (52)
Suppose that, instead of adding j to intarray to step
through the array addresses, you wanted to use the
increment operator. Could you write *(intarray++)?
The answer is no
Reason: Can’t increment a constant (or indeed
change it in any way).
◦ The expression intarray is the address where the system
has chosen to place your array, and it will stay at this
address until the program terminates.
◦ intarray is a pointer constant.
◦ One can’t say intarray++ any more than you can say 7++.
// array accessed with pointer
#include <iostream.h>
int main()
{
int intarray[] = { 31, 54, 77, 52, 93 }; //array
int* ptrint; //pointer to int
ptrint = intarray; //points to intarray
for(int j=0; j<5; j++) //for each element,
cout << *(ptrint++) << endl; //print value
return 0;
}
Three ways to pass arguments to a function:
◦ by value,
◦ by reference,
◦ and by pointer
If the function is intended to modify
variables in the calling program, then these
variables cannot be passed by value, since
the function obtains only a copy of the
variable.
Either a reference argument or a pointer can
be used in this situation.
// passref.cpp
// arguments passed by reference
#include <iostream.h>
int main()
{
double var = 10.0; //var has value of 10
inches
cout << “var = ” << var << “ inches” << endl;
centimize(var); //change var to centimeters
cout << “var = ” << var << “ centimeters” << endl;
return 0;
}
void centimize(double& v)
{
v *= 2.54; //v is the same as var
}
// arguments passed by pointer
#include <iostream.h>
int main()
{ double var = 10.0; //var has value of 10
inches
cout << “var = ” << var << “ inches” << endl;
centimize(&var); //change var to centimeters
cout << “var = ” << var << “ centimeters” << endl;
return 0;
}
void centimize(double* ptrd)
{
*ptrd *= 2.54; //*ptrd is the same as var
}
// passarr.cpp
// array passed by pointer
#include <iostream.h>
const int MAX = 5; //number of array elements
int main()
{
double varray[MAX] = { 10.0, 43.1, 95.9, 59.7, 87.3 };
centimize(varray); //change elements of varray to cm
for(int j=0; j<MAX; j++) //display new array values
cout << “varray[” << j << “]=” << varray[j] << “ centimeters”
<< endl; return 0; }
void centimize(double* ptrd)
{ for(int j=0; j<MAX; j++)
*ptrd++ *= 2.54; //ptrd points to elements of varray
}
// ptrorder.cpp
// orders two arguments using pointers
#include <iostream.h>
void order(int*, int*); //prototype
int main()
{
int n1=99, n2=11; //one pair ordered, one not
int n3=22, n4=88;
order(&n1, &n2); //order each pair of numbers
order(&n3, &n4);
cout << “n1=” << n1 << endl; //print out all
numbers
cout << “n2=” << n2 << endl;
cout << “n3=” << n3 << endl;
cout << “n4=” << n4 << endl;
return 0; }
void order(int* numb1, int* numb2) //orders two numbers
{ if(*numb1 > *numb2) //if 1st larger than
2nd,
{ int temp = *numb1; //swap them
*numb1 = *numb2;
*numb2 = temp; }
}
// ptrsort.cpp
// sorts an array using pointers
#include <iostream.h>
void bsort(int*, int); //prototype
void order(int*, int*); //prototype
int main()
{ const int N = 10; //array size test array
int arr[N] = { 37, 84, 62, 91, 11, 65, 57, 28, 19, 49 };
bsort(arr, N); //sort the array
for(int j=0; j<N; j++) //print out sorted array
cout << arr[j] << “ ”; cout << endl; return 0; }
void bsort(int* ptr, int n)
{ int j, k; //indexes to array
for(j=0; j<n-1; j++) //outer loop
for(k=j+1; k<n; k++) //inner loop starts at outer
order(ptr+j, ptr+k);} //order the pointer contents
void order(int* numb1, int* numb2) //orders two numbers
{ if(*numb1 > *numb2) //if 1st larger than 2nd,
{ int temp = *numb1; //swap them
*numb1 = *numb2;
*numb2 = temp; }
}