You are on page 1of 25

POINTERS

LECTURE 5
Introduction To Pointers

1.Accessing array elements


2.Passing arguments to a function when the
function needs to modify the original
argument (Passing By Reference)
3.Passing arrays and strings to functions
4.Obtaining memory from the system
5.Creating data structures such as linked
lists
Addresses and Pointers
Example:
1.#include <iostream>
2.using namespace std;
3.int main()
4.{
5.int var1 = 11; //define and initialize
6.int var2 = 22; //three variables
7.int var3 = 33;
8.cout << &var1 << endl //print the addresses
9. << &var2 << endl //of these variables
10. << &var3 << endl;
11.return 0;
12.}
Pointer Variables

A variable that holds an address value


is called a pointer variable, or simply a
pointer.

a pointer to int is not type int .


Example: Syntax Pointer Variable
1.#include <iostream>
2.using namespace std;
3.int main()
4.{
5.int var1 = 11; //two integer variables
6.int var2 = 22;
7.cout << &var1 << endl //print addresses of variables
8.<< &var2 << endl << endl;
9.int* ptr; //pointer to integers
10.ptr = &var1; //pointer points to var1
11.cout << ptr << endl; //print pointer value
12.ptr = &var2; //pointer points to var2
13.cout << ptr << endl; //print pointer value
14.return 0;
15.}
Accessing the Variable Pointed To
1.int main()
2.{
3.int var1 = 11; //two integer variables
4.int var2 = 22;
5.int* ptr; //pointer to integers
6.ptr = &var1; //pointer points to var1
7.cout << *ptr << endl; //print contents of pointer(11)
8.ptr = &var2; //pointer points to var2
9.cout << *ptr << endl; //print contents of pointer(22)
10.return 0;
11.}
Assignments to Pointers
#include <iostream>
using namespace std;
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;
}
Pointer to void
The address that you put in a pointer must be the same
type as the pointer. You can’t assign the address of a float
variable to a pointer to int.

1.float flovar = 98.6;


2.int* ptrint = &flovar; //ERROR: can’t assign
float* to int*

void* ptr; //ptr can point to any data type


Example:
1.#include <iostream>
2.using namespace std;
3.int main()
4.{
5.int intvar; //integer variable
6.float flovar; //float variable
7.int* ptrint; //define pointer to int
8.float* ptrflo; //define pointer to float
9.void* ptrvoid; //define pointer to void
10.ptrint = &intvar; ?
11.ptrint = &flovar; ?
12.ptrflo = &intvar; ?
13.ptrflo = &flovar; ?
14.ptrvoid = &intvar; ?
15.ptrvoid = &flovar; ?
Alternative:
If for some reason you really need to
assign one kind of pointer type to another,
you can use the reinterpret_cast.

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

1.Distance& dist = *(new Distance);

// In this approach we can use dot operator to access


member functions

You might also like