Ch10 Pointers
Ch10 Pointers
Object
Oriented
Object
Programming
Video
Lecture
Pointers
Engr. Rashid Farid Chishti
chishti@iiu.edu.pk
https://youtube.com/rfchishti
https://sites.google.com/site/chishti
International Islamic University H-10, Islamabad,
Pakistan
http://www.iiu.edu.pk 1
Pointers
Pointers are a special type of variables in which a
memory address is stored.
They contain a memory address, not the value of the
variable.
Pointers are an important and essential tool for
increasing the power of C++. A notable example is
the creation of data structures such as linked lists
and binary trees.
In fact, several key features of C++, such as virtual
functions, the new operator, and the this pointer
require the use of pointers.
Address and Pointers
The ideas behind pointers are not complicated. Here’s the first
key concept:
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 1KB of memory, the highest address is 1023. (Of
course you have much more.)
Your program, when it is loaded into memory, occupies a
certain range of these addresses.
That means that every variable and every function in your
program starts at a Particular address. You can find the
address occupied by a variable by using the address-of
operator &.
The Address-of Operator &
#include <iostream>
#include <stdlib.h>
using namespace std; 0x6ffe1
33
int main(){ 4 var3
.3
int var1 = 11; // define and
4
char var2 = 'A'; // initialize
float var3 = 33.34; // three variables
cout << "Address of Var1 = "
0x6ffe1b ‘A’ var2
<< &var1 << endl; 0x6ffe1 11
cout <<"Address of Var2 = " c var1
<< static_cast<void *>(&var2)
<< endl;
cout <<"Address of Var3 = "
<< &var3 << endl;
system("PAUSE");
return 0;
}
Pointer Variable
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int *ptr;
int var1 = 11;
int var2 = 22;
cout << "& ptr = " << &ptr << endl;
cout << "& var1 = " << &var1 << endl;
cout << "& var2 = " << &var2 << endl;
ptr = &var1; // pointer points
to var1
cout <<" ptr = " << ptr << endl; // print pointer
value
cout <<" *ptr = " << *ptr << endl; // print value of
var1
ptr = &var2; // pointer points
to var2
cout <<" ptr = " << ptr << endl; // print pointer
value
0x6ffe0 22 0x6ffe0 22
0 var2 0 var2
0x6ffe04 11 0x6ffe04 11
var1 var1
0x6ffe0 0x6ffe0
04
00
8 ptr 8 ptr
fe
fe
6f
6f
0x
0x
Pointer Variable
// other access using pointers
var1
#include <iostream>
#include <stdlib.h> 37
using namespace std;
int main() var2
{ 37
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 ptr
var2 = *ptr; // same as var2 = var1
cout << "var1 = " << var1 << endl; // verify var2 is 37
cout << "var2 = " << var2 << endl; // verify var2 is 37
system("PAUSE");
return 0; Output
}
var1 = 37
var2 = 37
Press any key to continue...
Pointer to void
// pointers to type void ptr_int int_var
#include <iostream>
#include <stdlib.h>
ptr_flo flo_var
using namespace std;
int main(){
int int_var; // integer variable
float flo_var; // float variable ptr_void
int* ptr_int; // define pointer to int
float* ptr_flo; // define pointer to float
void* ptr_void; // define pointer to void
ptr_int = &int_var; // ok, int* to int*
// ptr_int = &flo_var; // error, float* to int*
// ptr_flo = &int_var; // error, int* to float*
ptr_flo = &flo_var; // ok, float* to float*
ptr_void = &int_var; // ok, int* to void*
ptr_void = &flo_var; // ok, float* to void*
system("PAUSE"); return 0; }
Pointers and Arrays
// array accessed with pointer notation
#include <iostream>
#include <stdlib.h>
using namespace std; int_array
int main() 31
*(int_array +
*(int_array +
{
int intarray[5] = 54
{ 31, 54, 77, 52, 93 };
3)
4)
cout << *(intarray+j) << endl; 52
system("PAUSE");
return 0; 93
}
Pointer Arithmetic
// Adding and Subtracting a pointer variable
#include <iostream> ptr_int my_array
#include <stdlib.h>
using namespace std; 31 [0]
int main(){
int my_array[] = 54 [1]
{ 31, 54, 77, 52, 93 };
int* ptr;
77 [2]
// print at index 2; 31 77
cout << *ptr << " ";
Pointer Arithmetic