Week13 Pointer
Week13 Pointer
Topics
Pointers
Memory addresses
Declaration
Dereferencing a pointer
Pointers to pointer
Pointers and dynamic objects/ Slide 3
Computer Memory
Each variable is assigned a memory slot (the
size depends on the data type) and the
variable’s data is stored there
… … 100 … 1024 …
a
Variable a’s value, i.e., 100, is
int a = 100; stored at memory location 1024
Pointers and dynamic objects/ Slide 4
Pointers
A pointer is a variable used to store the
address of a memory cell.
We can use the pointer to reference this
memory cell
… … 100 … 1024 …
integer pointer
Pointers and dynamic objects/ Slide 5
Pointer Types
Pointer
C++ has pointer types for each type of object
Pointers to int objects
Pointers to char objects
Pointers to user-defined objects
(e.g., RationalNumber)
Even pointers to pointers
Pointers to pointers to int objects
Pointers and dynamic objects/ Slide 6
Pointer Variable
Examples:
int *n;
RationalNumber *r;
int **p; // pointer to pointer
Pointers and dynamic objects/ Slide 7
… … 100 … … …
a
int a = 100;
//get the value,
cout << a; //prints 100
//get the memory address
cout << &a; //prints 1024
Pointers and dynamic objects/ Slide 8
… 88 100 … … …
a b
#include <iostream>
using namespace std; Result is:
void main(){ The address of a is: 1020
int a, b; The address of b is: 1024
a = 88;
b = 100;
cout << "The address of a is: " << &a << endl;
cout << "The address of b is: " << &b << endl;
}
Pointers and dynamic objects/ Slide 9
Pointer Variables
Memory address: 1020 1024 1032
… 88 100 … 1024 …
a p
int a = 100; Result is:
int *p = &a; 100 1024
cout << a << " " << &a <<endl; 1024 1032
cout << p << " " << &p <<endl;
The value of pointer p is the address of variable a
A pointer is also a variable, so it has its own memory address
Pointers and dynamic objects/ Slide 10
Pointer to Pointer
58 58 58
Pointers and dynamic objects/ Slide 11
Dereferencing Operator *
We can access to the value stored in the variable
pointed to by using the dereferencing operator (*),
… 88 100 … 1024 …
a p
int a = 100;
int *p = &a;
Result is:
cout << a << endl; 100
cout << &a << endl; 1024
cout << p << " " << *p << endl; 1024 100
cout << &p << endl; 1032
Pointers and dynamic objects/ Slide 12
A Pointer Example
Memory Layout
The code
Box diagram
void doubleIt(int x, main
int * p)
{ p 8192
a 16 (8200)
*p = 2 * x;
} doubleIt
int main(int argc, const x 9
char * argv[]) (8196)
{
int a = 16;
a 16 main
doubleIt (8192)
doubleIt(9, &a);
return 0;
} x 9
a gets 18 p
Pointers and dynamic objects/ Slide 14
Reference Variables
A reference is an additional name to
an existing memory location
Pointer: Reference:
x 9 x
9
ref
ref
int x = 9;
int x=9;
int &ref = x;
int *ref;
ref = &x;
Pointers and dynamic objects/ Slide 17
Reference Variables
A reference variable serves as an alternative name for
an object
int m = 10;
int &j = m; // j is a reference variable
cout << “value of m = “ << m << endl;
//print 10
j = 18;
cout << “value of m = “ << m << endl;
// print 18
Pointers and dynamic objects/ Slide 18
Reference Variables
A reference variable always refers to the
same object. Assigning a reference variable
with a new value actually changes the value
of the referred object.
Reference variables are commonly used for
parameter passing to a function