0% found this document useful (0 votes)
3 views49 pages

Lesson 05 - 04. Pointers and Reference Variables - Explicit Address - 01

This document covers pointers and reference variables in C++ as part of an Object-Oriented Programming course. It includes topics such as memory types, reference variables, and pointers, along with examples demonstrating their usage. The instructor is Hieu Vo Dinh, and the session is scheduled for 120 minutes.

Uploaded by

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

Lesson 05 - 04. Pointers and Reference Variables - Explicit Address - 01

This document covers pointers and reference variables in C++ as part of an Object-Oriented Programming course. It includes topics such as memory types, reference variables, and pointers, along with examples demonstrating their usage. The instructor is Hieu Vo Dinh, and the session is scheduled for 120 minutes.

Uploaded by

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

05.

Pointers and
Reference Variables - 01

Oritented Object Programming C++: Chapter 04

 Time: 120 Minutes


 Instructor: Hieu, Vo Dinh
 Email: [email protected]
 Phone: 0941 891 998
Previous lessons

1. Introduction C++
 C++ vs C
 Compilers, IDEs
2. C++ Language Basics
 Types: int, float, double
 Control Structures: if, for, while, switch-case…
3. Functions, Arrays, Strings and Parameter Passing
 void func(int); void func(int, int =
1);
 int arr[10], brr[2][3];
 #include <cstring>; char str[10];
 #include <string>; string s;
Duy Tan University
2
Contents

I. Memory
 What is memory?
 Use operator & to get address of variables
II. References
 What is a reference?
 int a = 10; int& r = a;
 void iSwap(int &x, int &y) {…}
III. Pointers
 A pointer is a variable, used to store memory address
 int a = 10; int *p = &a;
 Derefering operator *

Duy Tan University


3
I. Memory

 There are three types:


Internal Processor Memory
• Placed within CPU
• Cache memory and special
registers
Primary Memory
• Know as main memory
• RAM and ROM
Secondary Memory
• Know as auxiliary memory

Duy Tan University


4
I. Memory (cont.)
0 1 1 10 ....
byte 1 1 1 0 0 1 1 11107
byte
0 1 0 0
1 0 1 11106
byte
0 0 1 1 1 0 0 11105
byte
1 1 1 0 ... ...
1 1 0 10104
byte
1 0 1 1 0 1 1 10103
byte
1 1 1 0
0 0 1 10102
byte
0 1 0 0 1 1 1 11 ....
byte 1 0 0 1 110
0 1 0 3 1
11 byte 1 0 0 0 1 0 2 0
11 byte 0 1 1
1 1 1 1 1
10 byte 1 1 1 1 0 1 0 1
10 byte 1 1 0 109
108
107
106
105
104
103
102
... ...
3
2
1
0

Duy Tan University


5
I. Memory (cont.)
 ... ...
char c;
113 1101 0111
 c = 'A'; //65 112
 short sh; 111
 sh = 1567; 110
 int i = 99; 109
 // Address-of operator(&) 108 00000000
 cout << &sh << endl; 107 00000000
99 i int
106 00000000
 cout << &i << endl;
105 01100011
 cout << (long long)(&c); 104 00000110
??? shor
1567 sh
103 00011111
??? t
Output Output Output 102
0x67 0x6dfeec 006DFEEC 101 'A'
01000001
??? c char
0x69 0x6dfee8 006DFEE8 ... ...
101 7208687 7208687
Duy Tan University
6
I. Memory (cont.)

 Address-of operator (&) is a unary operator that


returns the address of its operand
 &operand;
 Example:
 short s Output
= 1;
 int i = &s: 0x6dfeee
2; &i: 0x6dfee8
 float f = 2.5; &f: 0x6dfee4
 cout << "&s: " << &s << endl;
 cout << "&i: " << &i << endl;
 cout << "&f: " << &f << endl;

Duy Tan University


7
Contents

I. Memory
II. References
III. Pointers

Duy Tan University


8
II. References

 A reference variable is an alias, that is, another name


for an already existing variable.
 type& referenceName = declaredVariable;
 Example: Output ... ...
 int a = 1; a: 1 109
b: 30 108 b,
 int b = 2; &b: 0x6a 30
2 b
 107 ref
int& ref = b; ref: 30
 &ref: 0x6a 106
ref = 30;
105
 cout << "a: " << a << endl;
104
 cout << "b: " << b << endl;
103
 cout << "&b: " << &b << endl; 102 1 a
 cout << "ref: " << ref << endl;
101
 cout << "&ref: " << &ref; ... ...
Duy Tan University
9
Example 01

 Arguments passed by value and by reference


void func(short x, short& y) {
x = 20;
y = 40; Output
} a: 2
b: 40
int main() {
short a = 2, b = 4;
func(a, b);
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}

Duy Tan University


10
Example 01

 Arguments passed by value and by reference


void func(short x, short& y) {
x = 20; ... ...
y = 40; 109
} 108
int main() { 107
short a = 2, b = 4; 106
105
func(a, b);
104
cout << "a: " << a << endl; 4 b
103
cout << "b: " << b << endl; 102
2 a
return 0; 101
} ... ...

Duy Tan University


11
Example 01

 Arguments passed by value and by reference


void func(short x, short& y) {
x = 20; ... ...
y = 40; 109
} 108
int main() { 107
short a = 2, b = 4; 106
105
func(a, b);
104
cout << "a: " << a << endl; 4 b
103
cout << "b: " << b << endl; 102
2 a
return 0; 101
} ... ...

Duy Tan University


12
Example 01

 Arguments passed by value and by reference


void func(short x, short& y) {
short x = a, short& y =
b
x = 20; ... ...
y = 40; 109
} 108
int main() { 107
short a = 2, b = 4; 106
105 2 x
func(a, b);
104
104
cout << "a: " << a << endl; 103 4
4 b,b y
103
cout << "b: " << b << endl; 102
102 2 a
return 0; 101 2 a
101
} ...
... ...
...

Duy Tan University


13
Example 01

 Arguments passed by value and by reference


void func(short x, short& y) {
x = 20; ... ...
y = 40; 109
} 108
int main() { 107
short a = 2, b = 4; 106
20
2 x
105
func(a, b);
104
cout << "a: " << a << endl; 4 b, y
103
cout << "b: " << b << endl; 102
2 a
return 0; 101
} ... ...

Duy Tan University


14
Example 01

 Arguments passed by value and by reference


void func(short x, short& y) {
x = 20; ... ...
y = 40; 109
} 108
int main() { 107
short a = 2, b = 4; 106
20 x
105
func(a, b);
104
cout << "a: " << a << endl; 40
4 b, y
103
cout << "b: " << b << endl; 102
2 a
return 0; 101
} ... ...

Duy Tan University


15
Example 01

 Arguments passed by value and by reference


void func(short x, short& y) {
x = 20; ... ...
y = 40; 109
} 108
int main() { 107
short a = 2, b = 4; 106
20 x
105
func(a, b);
104
cout << "a: " << a << endl; 40 b,b y
103
cout << "b: " << b << endl; 102
2 a
return 0; 101
} ... ...

Duy Tan University


16
Example 01

 Arguments passed by value and by reference


void func(short x, short& y) {
x = 20; ... ...
y = 40; 109
Output
} 108
a: 2
int main() { 107
short a = 2, b = 4; 106
105
func(a, b);
104
cout << "a: " << a << endl; 40 b
103
cout << "b: " << b << endl; 102
2 a
return 0; 101
} ... ...

Duy Tan University


17
Example 01

 Arguments passed by value and by reference


void func(short x, short& y) {
x = 20; ... ...
y = 40; 109
Output
} 108
a: 2
int main() { 107
b: 40
short a = 2, b = 4; 106
105
func(a, b);
104
cout << "a: " << a << endl; 40 b
103
cout << "b: " << b << endl; 102
2 a
return 0; 101
} ... ...

Duy Tan University


18
Example 02

 Write a function to swap two numbers


void mySwap(int& x, int& y) {
int temp = x;
x = y; y = temp;
} Output
int main() { a: 2
int a = 1, b = 2; b: 1
mySwap(a, b);
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}

Duy Tan University


19
Example 03

 Return by reference
short& myMax(short& x, short& y) {
if (x > y)
return x;
return y; // short& kq = y; Output
} a: 2
int main() { b: 99
short a = 2, b = 4;
myMax(a, b) = 99;
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
}
Duy Tan University
20
Example 04

 Return by reference
short& myMax(short& x, short& y) {
short m = x;
if (y > x) m = y;
return m; Output
} a: 2
int main() { b: 4
c: -22128
short a = 2, b = 4;
short& c = myMax(a, b);
cout << "a: " << a << endl;
cout << "b: " << b << endl;
cout << "c: " << c << endl;
return 0;
} 21 Duy Tan University
Example 05

 const reference arguments


int sum(const int& x, const int& y)
{
x = 10;
return x + y; Output
} 3
int main() {
int a = 1, b = 2;
cout << sum(a, b) << endl;
return 0;
}

Duy Tan University


22
Contents

I. Memory
II. References
III. Pointers

Duy Tan University


23
III. Pointers

 A pointer variable is a variable, used to store a


memory address Output ... ...
pA is point to a
type* pName; a: 2 0x71
&a: 0x67 0x70
 Example 01: pA: 0x67
0x6f
&pA: 0x6c
 short a = 2; size: 4 0x6e
???
0x67 pA
 short* pA; 0x6d
 pA = &a; 0x6c
 cout << "a: " << a << endl; 0x6b
0x6a
 cout << "&a: " << &a << endl;
0x69
 cout << "pA: " << pA << endl; 0x68
 cout << "&pA: " << &pA << endl; 2 a
0x67
 cout << "size: " << sizeof(pA);... ...

Duy Tan University


24
III. Pointers (cont.)

 Example 02: What are the pointer variables?


 int* p1; Should use
 int *p2;
 int* p3, p4;
 int *p5, *p6;

p4 is an integer

Duy Tan University


25
III. Pointers (cont.)

 Dereferencing operator (*)


Return a reference to address which is stored in the
pointer variable
*pointerName;
Output
 Example 02: 0x67 pA
*pa: 2
short a = 2;
a: 10 0x6c
short* pA = &a;
cout << "*pa: " << *pA << endl;
*pA = 10;
cout << "a: " << a << endl; 2 a
a
0x67
0x67 10
*pA

Duy Tan University


26
III. Pointers (cont.)
... ...
 Example 03: Output 0x71
#include <iostream> 1000000 0x70
using namespace std; 16960 0x6f
int main() 0x6e
{ 0x6d
0x6c
int i = 1000000;
0x6b
short* pShort = 0x6a
(short*)&i; 0x69
cout << i << endl; 0x68
cout << *pShort << endl; 0x67
return 0; 0x66
} 0x65
0x64
... ...
Duy Tan University
27
III. Pointers (cont.)
... ...
 Example 03:
0x71
#include <iostream> 0x70
using namespace std; 0x6f
int main() 0x6e
{ 0x6d
0x6c
int i = 1000000;
0x6b
short* pShort = 0x6a
(short*)&i; 0x69
cout << i << endl; 0x68 0000000
0x68
cout << *pShort << endl; 0x67 0
return 0; 0x66 0000111
0x67
0x65 1
} i
0x64 0100001
0x66
0
... ...
28 0100000
Duy Tan University
0x65
III. Pointers (cont.)
... ...
 Example 03:
0x71
#include <iostream> 0x70 pShor
0x65
using namespace std; 0x6f t
int main() 0x6e
{ 0x6d
0x6c
int i = 1000000;
0x6b
short* pShort = 0x6a
(short*)&i; 0x69
cout << i << endl; 0000000
0x68
cout << *pShort << endl; 0
return 0; 0000111
0x67
1
} i
0100001
0x66
0
29 0100000
Duy Tan University
0x65
III. Pointers (cont.)
... ...
 Example 03: Output 0x71
#include <iostream> 1000000 0x70 pShor
0x65
using namespace std; 0x6f t
int main() 0x6e
{ 0x6d
0x6c
int i = 1000000;
0x6b
short* pShort = 0x6a
(short*)&i; 0x69
cout << i << endl; 0000000
0x68
cout << *pShort << endl; 0
return 0; 0000111
0x67
1
} i
0100001
0x66
0
30 0100000
Duy Tan University
0x65
III. Pointers (cont.)
... ...
 Example 03: Output 0x71
#include <iostream> 1000000 0x70 pShor
0x65
using namespace std; 16960 0x6f t
int main() 0x6e
{ 0x6d
0x6c
int i = 1000000;
0x6b
short* pShort = 0x6a
(short*)&i; 0x69
cout << i << endl; 0000000
0x68
cout << *pShort << endl; 0
return 0; 0000111
0x67
1
} i
0100001
0x66
0
31 0100000
Duy Tan University
0x65
Contents

I. Memory
II. References
III. Pointers

Duy Tan University


32
Example 01
Output Output
#include <iostream>
using namespace std; &a: 0x6dfeea
int main() { *&a: 2
&*&a: 0x6dfeea
short a = 2;
*&*&a: 2
short* pA = &a;
pA: 0x6dfeea
cout << "&a: " << &a << endl; *pA: 2
cout << "*&a: " << *&a << endl; &*pA: 0x6dfeea
cout << "&*&a: " << &*&a << endl;
cout << "*&*&a: " << *&*&a << endl;
cout << "pA: " << pA << endl;
cout << "*pA: " << *pA << endl;
cout << "&*pA: " << &*pA << endl;
return 0;
} 33 Duy Tan University
Example 02
Output
#include <iostream>
using namespace std; a: 2
int main() *pA: 2
a: 30
{
*pA: 30
short a = 2;
short* pA = &a;
cout << "a: " << a << endl;
cout << "*pA: " << *pA << endl;
*pA = 30;
cout << "a: " << a << endl;
cout << "*pA: " << *pA << endl;
return 0;
}
Duy Tan University
34
Example 03
Output
#include <iostream>
using namespace std;
pA: 0x4270de
int main()
*pA: -15229
{
short* pA;
cout << "pA: " << pA << endl;
cout << "*pA: " << *pA << endl;
return 0;
}

Duy Tan University


35
Example 04
Output
#include <iostream>
using namespace std;
pChar: 4
int main()
pShort: 4
{ pLong: 4
char* pChar; pLongLong: 4
short* pShort;
long* pLong;
long long* pLongLong;
cout << "pChar: " << sizeof(pChar) << endl;
cout << "pShort: " << sizeof(pShort) <<
endl;
cout << "pLong: " << sizeof(pLong) << endl;
cout << "pLongLong: " << sizeof(pLongLong);
return 0; Duy Tan University
36
Example 05
#include <iostream> Output
using namespace std;
void mySwap(short* x, short* y) { a: 5
b: 3
short tmp = *x;
*x = *y;
*y = tmp;
}
int main() {
short a = 3, b = 5;
mySwap(&a, &b);
cout << "a: " << a << endl;
cout << "b: " << b << endl;
return 0;
} 37 Duy Tan University
Example 05 (cont.)
... ...
#include <iostream>
0x71
using namespace std;
0x70
void mySwap(short* x, short* y) { 0x6f
short tmp = *x; 0x6e
*x = *y; 0x6d
*y = tmp; 0x6c
} 0x6b
0x6a
int main() {
0x69
short a = 3, b = 5; 0x68
mySwap(&a, &b); 0x67
cout << "a: " << a << endl; 0x66
cout << "b: " << b << endl; 0x65
return 0; 0x64
... ...
} 38 Duy Tan University
Example 05 (cont.)
... ...
#include <iostream>
0x71
using namespace std;
0x70
void mySwap(short* x, short* y) { 0x6f
short tmp = *x; 0x6e
*x = *y; 0x6d
*y = tmp; 0x6c
} 0x6b
0x6a
int main() {
0x69
short a = 3, b = 5; 0x68
mySwap(&a, &b); 0x67
5 b
cout << "a: " << a << endl; 0x66
cout << "b: " << b << endl; 0x65 3 a
return 0; 0x64
... ...
} 39 Duy Tan University
Example 05 (cont.)
... ...
#include <iostream>
0x71
using namespace std;
0x70
void mySwap(short* x, short* y) { 0x6f
short tmp = *x; 0x6e
*x = *y; 0x6d
*y = tmp; 0x6c
} 0x6b
0x6a
int main() {
0x69
short a = 3, b = 5; 0x68
mySwap(&a, &b); 0x67
5 b
cout << "a: " << a << endl; 0x66
cout << "b: " << b << endl; 0x65 3 a
return 0; 0x64
... ...
} 40 Duy Tan University
Example 05 (cont.)
short* x = &a, short* y =
&b ... ...
#include <iostream>
0x71
using namespace std;
0x70
void mySwap(short* x, short* y) { 0x6f
short tmp = *x; 0x6e
0x66 y
*x = *y; 0x6d
*y = tmp; 0x6c
0x6c
} 0x6b
0x6b
0x6a
0x6a
int main() { 0x64 x
0x69
0x69
short a = 3, b = 5; 0x68
0x68
mySwap(&a, &b); 0x67
0x67 5 b
cout << "a: " << a << endl; 0x66 5 b
0x66
cout << "b: " << b << endl; 0x65
0x65 3 a
return 0; 0x64 3 a
0x64
... ...
} 41
... ...Duy Tan University
Example 05 (cont.)
... ...
#include <iostream>
0x71
using namespace std; 3 tmp
0x70
void mySwap(short* x, short* y) { 0x6f
short tmp = *x; 0x6e
0x66 y
*x = *y; 0x6d
*y = tmp; 0x6c
} 0x6b
0x6a
int main() { 0x64 x
0x69
short a = 3, b = 5; 0x68
mySwap(&a, &b); 0x67
5 b
cout << "a: " << a << endl; 0x66
cout << "b: " << b << endl; 0x65 3 a
return 0; 0x64
... ...
} 42 Duy Tan University
Example 05 (cont.)
... ...
#include <iostream>
0x71
using namespace std; 3 tmp
0x70
void mySwap(short* x, short* y) { 0x6f
short tmp = *x; 0x6e
0x66 y
*x = *y; 0x6d
*y = tmp; 0x6c
} 0x6b
0x6a
int main() { 0x64 x
0x69
short a = 3, b = 5; 0x68
mySwap(&a, &b); 0x67
5 b
cout << "a: " << a << endl; 0x66
cout << "b: " << b << endl; 0x65 3
5 a
return 0; 0x64
... ...
} 43 Duy Tan University
Example 05 (cont.)
... ...
#include <iostream>
0x71
using namespace std; 3 tmp
0x70
void mySwap(short* x, short* y) { 0x6f
short tmp = *x; 0x6e
0x66 y
*x = *y; 0x6d
*y = tmp; 0x6c
} 0x6b
0x6a
int main() { 0x64 x
0x69
short a = 3, b = 5; 0x68
mySwap(&a, &b); 0x67
5
3 b
cout << "a: " << a << endl; 0x66
cout << "b: " << b << endl; 0x65 5 a
return 0; 0x64
... ...
} 44 Duy Tan University
Example 05 (cont.)
... ...
#include <iostream>
0x71
using namespace std; 3 tmp
0x70
void mySwap(short* x, short* y) { 0x6f
short tmp = *x; 0x6e
0x66 y
*x = *y; 0x6d
*y = tmp; 0x6c
} 0x6b
0x6a
int main() { 0x64 x
0x69
short a = 3, b = 5; 0x68
mySwap(&a, &b); 0x67
3 b
cout << "a: " << a << endl; 0x66
cout << "b: " << b << endl; 0x65 5 a
return 0; 0x64
... ...
} 45 Duy Tan University
Example 05 (cont.)
... ...
#include <iostream>
0x71
using namespace std; 0x70
void mySwap(short* x, short* y) { 0x6f
short tmp = *x; Output 0x6e
*x = *y; 0x6d
*y = tmp; a: 5 0x6c
} b: 3 0x6b
0x6a
int main() {
0x69
short a = 3, b = 5; 0x68
mySwap(&a, &b); 0x67
3 b
cout << "a: " << a << endl; 0x66
cout << "b: " << b << endl; 0x65 5 a
return 0; 0x64
... ...
} 46 Duy Tan University
Contents

I. Memory
II. References
III. Pointers

Duy Tan University


47
Summary

 A reference variable is an alias, that is, another name


for an already existing variable
 Address-of operator (&) get address of a variable
 A pointer variable is a variable, used to store a
memory address
 Dereferencing operator (*) return a lvalue
 Pass-By-Reference with Reference Arguments
void mySwap(short& x, short& y);
 Pass-By-Reference with Pointer Arguments
void mySwap(short* x, short* y);

Duy Tan University


48
Problems

1. Use references to write rSwap(…) functions to


swap values of two integers, two floats, two
doubles.
2. Use pointers to write pSwap(…) functions to swap
values of two integers, two floats, two doubles.
3. Use references to write input(…), display(…)
functions to enter and display: an integer, a vector, a
string.
4. Use pointer to write input(…), display(…)
functions to enter and display: an integer, a vector, a
string.
Duy Tan University
49

You might also like