0% found this document useful (0 votes)
15 views

Week13 Pointer

The document discusses pointers, which are variables that store memory addresses. It covers declaring pointers, dereferencing pointers to access the value at a memory address, pointers to pointers, and references which serve as alternative names for objects.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Week13 Pointer

The document discusses pointers, which are variables that store memory addresses. It covers declaring pointers, dereferencing pointers to access the value at a memory address, pointers to pointers, and references which serve as alternative names for objects.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Pointers

Pointers and dynamic objects/ Slide 2

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

emory address: 1020 1024 1032

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

Memory address: 1020 1024 1032

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

 Declaration of Pointer variables


type* pointer_name;
//or
type *pointer_name;
where type is the type of data pointed to (e.g. int, char, double)

Examples:
int *n;
RationalNumber *r;
int **p; // pointer to pointer
Pointers and dynamic objects/ Slide 7

Address Operator &


 The "address of " operator (&) gives the memory
address of the variable
 Usage: &variable_name

Memory address: 1020 1024

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

Address Operator &


Memory address: 1020 1024 1032

… 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

What is the output?

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 (*),

Memory address: 1020 1024 1032

… 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

Don’t get confused


 Declaring a pointer means only that it is a pointer: int
*p;
 Don’t be confused with the dereferencing operator, which
is also written with an asterisk (*). They are simply two
different tasks represented with the same sign
int a = 100, b = 88, c = 8;
int *p1 = &a, *p2, *p3 = &c;
p2 = &b; // p2 points to b
p2 = p1; // p2 points to a
b = *p3; //assign c to b
*p2 = *p3; //assign c to a  Result is:
cout << a << b << c; 888
Pointers and dynamic objects/ Slide 13

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

Another Pointer Example


#include <iostream>
 Result is
using namespace std;
int main (){ value1==10 / value2==20
int value1 = 5, value2 = 15;
int *p1, *p2;
p1 = &value1; // p1 = address of value1
p2 = &value2; // p2 = address of value2
*p1 = 10; // value pointed to by p1=10
*p2 = *p1; // value pointed to by p2= value // pointed
to by p1
p1 = p2; // p1 = p2 (pointer value copied)
*p1 = 20; // value pointed to by p1 = 20
cout << "value1==" << value1 << "/ value2==" << value2;
return 0;
}
Pointers and dynamic objects/ Slide 15

Another Pointer Example


int a = 3;
char s = ‘z’;
double d = 1.03;
int *pa = &a;
char *ps = &s;
double *pd = &d;
cout << sizeof(pa) << sizeof(*pa)
<< sizeof(&pa) << endl;
cout << sizeof(ps) << sizeof(*ps)
<< sizeof(&ps) << endl;
cout << sizeof(pd) << sizeof(*pd)
<< sizeof(&pd) << endl;
Pointers and dynamic objects/ Slide 16

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

You might also like