0% found this document useful (0 votes)
86 views27 pages

Ch10 - Pointers Revision

This document discusses pointers in C++. It begins by explaining that pointers store memory addresses and allow variables to indirectly reference locations in memory. It then provides examples of declaring pointer variables, assigning addresses to pointers, accessing the values pointed to via dereferencing, and passing pointers to functions. The document also discusses using pointers to modify original arguments, access array elements, and sort arrays. Overall it serves as a tutorial on the basic concepts and uses of pointers in C++.

Uploaded by

Mohsin Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views27 pages

Ch10 - Pointers Revision

This document discusses pointers in C++. It begins by explaining that pointers store memory addresses and allow variables to indirectly reference locations in memory. It then provides examples of declaring pointer variables, assigning addresses to pointers, accessing the values pointed to via dereferencing, and passing pointers to functions. The document also discusses using pointers to modify original arguments, access array elements, and sort arrays. Overall it serves as a tutorial on the basic concepts and uses of pointers in C++.

Uploaded by

Mohsin Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Engr Saiqa

 Accessing array elements


 Passing arguments to a function when the

function needs to modify the original


argument
 Passing arrays and strings to functions
 Obtaining memory from the system
 Creating data structures such as linked lists
 The ideas behind pointers are not
complicated:
◦ Every byte in the computer’s memory has an
address.
◦ Addresses are numbers, just as they are for
houses on a street. The numbers start at 0 and go
up from there—1, 2, 3, and so on.
◦ If you have 1MB of memory, the highest address
is 1,048,575. (Of course you probably have much
more.)
 Program, when it is loaded into memory,
occupies a certain range of these addresses.
i.e. every variable and every function in a
program starts at a particular address
// varaddr.cpp
// addresses of variables
#include <iostream.h>
int main()
{
int var1 = 11; //define and initialize
int var2 = 22; //three variables
int var3 = 33;
cout << &var1 << endl //print the addresses
<< &var2 << endl //of these variables
<< &var3 << endl;
return 0;
}
 Addresses by themselves are rather limited.
 The potential for increasing our

programming power requires an additional


idea: variables that hold address values.
 A variable that holds an address value is

called a pointer variable, or simply a


pointer.
 What is the data type of pointer variables?

◦ It’s the same as the variable whose address is


being stored; a pointer to int is type int.
// pointers (address variables)
#include <iostream.h>
int main()
{
int var1 = 11; //two integer variables
int var2 = 22;
cout << &var1 << endl //print addresses of variables
<< &var2 << endl << endl;
int* ptr; //pointer to integers
ptr = &var1; //pointer points to var1
cout << ptr << endl; //print pointer value
ptr = &var2; //pointer points to var2
cout << ptr << endl; //print pointer value
return 0;
}
 An address like 0x8f4ffff4 can be thought of as a
pointer constant
 A pointer can hold the address of any variable of
the correct type; it’s a receptacle awaiting an
address.
 It must be given some value, otherwise it will point
to an address we don’t want it to point to, such as
into our program code or the operating system.
 Rogue pointer values can result in system crashes
and are difficult to debug, since the compiler gives
no warning.
 The moral: Make sure you give every pointer
variable a valid address value before using it.
// ptracc.cpp
// accessing the variable pointed to
#include <iostream.h>
int main()
{
int var1 = 11; //two integer variables
int var2 = 22;
int* ptr; //pointer to integers
ptr = &var1; //pointer points to var1
cout << *ptr << endl; //print contents of pointer (11)
ptr = &var2; //pointer points to var2
cout << *ptr << endl; //print contents of pointer (22)
return 0;
}
 When an asterisk is used in front of a variable name, as it is in the
*ptr expression, it is called the indirection operator.
 It means the value of the variable pointed to by. Thus the expression
*ptr represents the value of the variable pointed to by ptr
 You can use a pointer not only to display a variable’s value, but
also to perform any operation you would perform on the
variable directly

#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>

void centimize(double&); //prototype

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>

void centimize(double*); //prototype

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

void centimize(double*); //prototype

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; }
}

You might also like