1
CHAPTER 1
POINTERS (PART 1)
●2
Chapter’s Outcome
• To understand the concept of pointer and
address in C++
• To declare a pointer variables and pointer
• To learn on how to reference and de-
reference a pointer
3
Chapter’s Outline
• Pointers
o Computer Memory
o Variable
o Pointer Variable
o Address Operation &
o Pointer to Pointer
o Dereferencing Operator
o NULL Pointer
4
Computer Memory
• Each variable is assigned a memory slot (the size
depends on the data type) and the variable’s
data is stored there
Memory address: 1020 1024 1032
… … 100 … 1024 …
a
int a = 100; Variable a’s value is 100 and
stored at memory location 1024
5
Variable
• A variable is a named memory location in a
program that holds a value of a particular data
type. It has an identifier (i.e., name) and a value
that can be read or modified throughout the
program's execution.
• For example,
int num1 = 100;
int num2 = 130;
6
Pointer Variable
• 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
7
Major advantages of pointers are:
1. It allows management of structures which are
allocated memory dynamically.
2. It allows passing of arrays and strings to functions
more efficiently.
3. It makes possible to pass address of structure
instead of entire structure to the functions.
8
Pointer Types
• Pointer
• C++ has pointer types for each type of object
• Pointers to int objects
• Pointers to char objects
• Even pointers to pointers
• Pointers to pointers to int objects
9
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)
• For examples:
int *n; // pointer to integer
char *ch; // pointer to char
int **p; // pointer to pointer
10
Pointer Variable
• For example:
int num1 = 100;
int *numPtr;
11
Address Operator &
• The address operator ”&” gives the memory address
of the variable.
• For example &variable_name
Memory address: 1020 1024
… … 100 … … …
num1
int num1 = 100;
//get the value,
cout << num1; //prints 100
//get the memory address
cout << &num1; //prints 1024
12
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
The address of b is: 1024
int a, b;
a = 88;
b = 100;
cout << "The address of a is: " << &a << endl;
cout << "The address of b is: " << &b << endl;
}
13
Address Operator &
#include <iostream>
using namespace std;
void main(){
int a, b;
a = 88;
b = 100;
cout << "The address of a is: " << &a << endl;
cout << "The address of b is: " << &b << endl;
}
14
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
15
Pointer to Pointer
What is the output?
58 58 58
16
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
17
Dereferencing Operator *
Dereferencing Operator *
• a variable which stores the address of another variable is
called a pointer. Pointers are said to "point to" the variable
whose address they store.
• An interesting property of pointers is that they can be used to
access the variable they point to directly. This is done by
preceding the pointer name with the dereference operator
(*). The operator itself can be read as "value pointed to by".
• Therefore, following with the values of the previous example,
the following statement:
num1 = *p;
●18
Dereferencing Operator *
The reference and dereference operators are
complementary:
• & is the address-of operator, and can be read
simply as "address of"
• * is the dereference operator, and can be read as
"value pointed to by"
Thus, they have sort of opposite meanings.
num1 = p; // num1 equal to p
num1 = &p; // num1 equal to address of p
num1 = *p; // num1 equal to value pointed to by p
●19
20
Dereferencing Operator (*)
• 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
cout << a << b << c;
Address 70 a=100 p1=70 p2= p3=72
Address 71 b=88
Address 72 c=8
21
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
cout << a << b << c;
Address 70 a=100 p1=70 p2=71 p3=72
Address 71 b=88
Address 72 c=8
22
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
cout << a << b << c;
Address 70 a=100 p1=70 p2=70 p3=72
Address 71 b=88
Address 72 c=8
23
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
cout << a << b << c;
Address 70 a=100 p1=70 p2=70 p3=72
Address 71 b=8 *p3 = 8,
Address 72 c=8 So b = 8
24
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
cout << a << b << c;
Address 70 a=8 p1=70 p2=70 p3=72
Address 71 b=8 *p2 = *p3 , where *p3 = 8
*p2 is refer to a
Address 72 c=8 So a= 8
25
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
26
NULL pointer
• NULL is a special value that indicates an empty pointer.
• If you try to access a NULL pointer, you will get an error.
int *p;
p = 0;
cout << p << endl; //prints 0
cout << &p << endl;//prints address of p
cout << *p << endl;//Error!
27
CHAPTER 1
POINTERS (PART 2)
Example 1: Pointer Addition
Address
1001 1002 1003 1004
int a;
int b; a=7 b P=1001 q=1001
int* p;
int* q;
a = 3;
p = &a;
q = p; *q is dereferencing
*q = *q+4; q, so q is referring
cout << *p; to a
Example 1: Pointer Addition
Address
1001 1002 1003 1004
int a;
int b; a=7 b P=1001 q=1001
int* p;
int* q;
a = 3;
p = &a;
q = p; *q is dereferencing
*q = *q+4; q, so q is referring
cout << *p; to a 7
*p is dereferencing p,
so p is referring to a
Example 2: Pointer Addition
Address
1001 1002 1003 1004
int a;
int b; a=3 b=100 P=1001 q=1001
int* p;
int* q;
a = 3; b=100;
p = &a;
q = p;
*q = *q++;
cout << *p<<endl;
Cout << *q;
Example 2: Pointer Addition
Address
1001 1002 1003 1004
int a;
int b; a=3 b=100 P=1001 q=1002
int* p;
int* q;
a = 3; b=100;
p = &a;
q = p;
Do post increment first, then
*q = *q++; dereferencing…
cout << *p Post increment of q…
<<endl; q=1002
Cout << *q; Dereferencing q, so q is
referring to b
Example 2: Pointer Addition
Address
1001 1002 1003 1004
int a;
int b; a=3 b=100 P=1001 q=1002
int* p;
int* q;
a = 3; b=100;
p = &a;
q = p; Do post increment first, then
*q = *q++; dereferencing… 3
cout << *p; Post increment of q…
<<endl q=1002
Cout << *q; Dereferencing q, so q is
referring to b
Example 2: Pointer Addition
Address
1001 1002 1003 1004
int a;
int b; a=3 b=100 P=1001 q=1002
int* p;
int* q;
a = 3; b=100;
p = &a;
q = p; Do post increment first, then
*q = *q++; dereferencing… 3
cout << Post increment of q… 100
*p<<endl; q=1002
Cout << *q; Dereferencing q, so q is
referring to b