Professional Documents
Culture Documents
LECTURE 5
Introduction To Pointers
1.ptrint = reinterpret_cast<int*>(flovar);
2.ptrflo = reinterpret_cast<float*>(intvar);
Pointers and Arrays
Program Using ARRAY
1.#include <iostream>
2.using namespace std;
3.int main()
4.{ //array
5.int intarray[5] = { 31, 54, 77, 52, 93 };
6.for(int j=0; j<5; j++) //for each element,
7.cout << intarray[j] << endl; //print value
8.return 0;
9.}
Previous Program Using Pointer
1.#include <iostream>
2.using namespace std;
3.int main()
4.{ //array
5.int intarray[5] = { 31, 54, 77, 52, 93 };
6.for(int j=0; j<5; j++) //for each element,
7.cout << *(intarray+j) << endl; //print value
8.return 0;
9.}
Pointer Constants and Pointer
Variables
1.#include <iostream>
2.using namespace std;
3.int main()
4.{
5.int intarray[] = { 31, 54, 77, 52, 93 }; //array
6.int* ptrint; //pointer to int
7.ptrint = intarray; //points to intarray
8.for(int j=0; j<5; j++) //for each element,
9.cout << *(ptrint++) << endl; //print value
10.return 0;
11.}
Pointers and Functions
1.#include <iostream>
2.using namespace std;
3.int main()
4.{
5.void centimize(double&); //prototype
6.double var = 10.0; //var has value of 10 inches
7.cout << “var = ” << var << “ inches” << endl;
8.centimize(var); //change var to centimeters
9.cout << “var = ” << var << “ centimeters” << endl;
10.return 0;
11.}
12.void centimize (double& v)
13.{
14.v *= 2.54; //v is the same as var
15.}
Example Of Arguments Passed By
Pointers
1.#include <iostream>
2.using namespace std;
3.int main()
4.{
5.void centimize(double*); //prototype
6.double var = 10.0; //var has value of 10 inches
7.cout << “var = ” << var << “ inches” << endl;
8.centimize(&var); //change var to centimeters
9.cout << “var = ” << var << “ centimeters” << endl;
10.return 0;
11.} void centimize(double* ptrd)
12.{
13.*ptrd *= 2.54; //*ptrd is the same as var
14.}
Passing Arrays
1.const int MAX = 5; //number of array elements
2.int main()
3.{
4.void centimize(double*); //prototype
5.double varray[MAX] = { 10.0, 43.1, 95.9, 59.7, 87.3 };
6.centimize(varray); //change elements of varray to
cm
7.for(int j=0; j<MAX; j++) //display new array values
8.cout << “varray[” << j << “]=”
9.<< varray[j] << “ centimeters” << endl;
10.return 0;
11.} void centimize(double* ptrd)
12.{
13.for(int j=0; j<MAX; j++)
14.*ptrd++ *= 2.54; //ptrd points to elements of
varray
Graphical Representation
Sorting Array Elements
1.#include <iostream>
2.using namespace std;
3.int main()
4.{
5.void order(int*, int*); //prototype
6.int n1=99, n2=11; //one pair ordered, one not
7.int n3=22, n4=88;
8.order(&n1, &n2); //order each pair of numbers
9.order(&n3, &n4);
10.cout << “n1=” << n1 << endl; //print out all
numbers
11.cout << “n2=” << n2 << endl;
12.cout << “n3=” << n3 << endl;
13.cout << “n4=” << n4 << endl;
14.return 0;
Contd..
1.void order(int* numb1, int* numb2) //orders two
numbers
2.{
3.if(*numb1 > *numb2) //if 1st larger than 2nd,
4.{
5.int temp = *numb1; //swap them
6.*numb1 = *numb2;
7.*numb2 = temp;
8.}
9.}
Second Example:
#include <iostream>
using namespace std;
int main()
{
void bsort(int*, int); //prototype
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;
}
Contd..
1.void bsort(int* ptr, int n)
2.{
3.void order(int*, int*); //prototype
4.int j, k; //indexes to array
5.for(j=0; j<n-1; j++) //outer loop
6.for(k=j+1; k<n; k++) //inner loop starts at outer
7.order(ptr+j, ptr+k); //order the pointer contents
8.}
9. void order(int* numb1, int* numb2) //orders two
numbers
10.{
11.if(*numb1 > *numb2) //if 1st larger than 2nd,
12.{
13.int temp = *numb1; //swap them
14.*numb1 = *numb2;
Pointers to Objects
#include <iostream>
using namespace std;
class Distance //English Distance class
{
private:
int feet;
float inches;
public:
void getdist() //get length from user
{
cout << “\nEnter feet: ”; cin >> feet;
cout << “Enter inches: ”; cin >> inches;
}
void showdist() //display distance
{ cout << feet << “\’-” << inches << ‘\”’; }
int main()
Contd..
{
Distance dist; //define a named Distance object
dist.getdist(); //access object members
dist.showdist(); // with dot operator
Distance* distptr; //pointer to Distance
distptr = new Distance; //points to new Distance object
distptr->getdist(); //access object members
distptr->showdist(); // with -> operator
cout << endl;
return 0;
}
Another Approach
1.(*distptr).getdist(); // ok but inelegant