AN OUTCOMES-BASED APPROACH ON Data structures and Algorithm
2 hours lecture & 3 hours laboratory
DATA STRUCTURES AND ALGORITHM Version1
Module 4: Pointers
I. INTRODUCTION:
Some C++ programming tasks are performed more easily with pointers, and other tasks, such
as dynamic memory allocation, cannot be performed without using pointers. So it becomes
necessary to learn pointers to become a perfect C++ programmer. Let's start learning them in
simple and easy steps.
As you know every variable is a memory location and every memory location has its address
defined which can be accessed using ampersand (&) operator which denotes an address in
memory. Consider the following which will print the address of the variables defined
II. LEARNING OBJECTIVES
At the end of the lesson, the students are expected to:
Learn about Pointers in C++
Learn the similarities and differences between pointers and references
Use pointers to pass arguments to function by reference
Understand the close relationships between pointers and built-in arrays
Use pointer-based strings
Use built-in arrays
III. LEARNING CONTENT
A. TOPIC DISCUSSIONS
WHAT IS POINTERS?
A pointer is a variable whose value is the address of another variable. Like any variable or
constant, you must declare a pointer before you can work with it. The general form of a pointer
variable declaration is –
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the
name of the pointer variable. The asterisk you used to declare a pointer is the same asterisk that
you use for multiplication. However, in this statement the asterisk is being used to designate a
variable as a pointer. Following are the valid pointer declaration –
AN OUTCOMES-BASED APPROACH ON Data structures and Algorithm
2 hours lecture & 3 hours laboratory
DATA STRUCTURES AND ALGORITHM Version1
The actual data type of the value of all pointers, whether integer, float, character, or
otherwise, is the same, a long hexadecimal number that represents a memory address. The
only difference between pointers of different data types is the data type of the variable or
constant that the pointer points to.
Example 1: Pointer operators & and *
#include <iostream>
using namespace std;
int main () {
int var = 20; // actual variable declaration.
int *ip; // pointer variable
ip = &var; // store address of var in pointer variable
cout << "Value of var variable: ";
cout << var << endl;
// print the address stored in ip pointer variable
cout << "Address stored in ip variable: ";
cout << ip << endl;
// access the value at the address available in pointer
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0;
}
When the above code is compiled and executed, it produces result something as follows –
Example 2: Using the Address (&) and Indirection (*) Operators
The program in below demonstrates the & and * pointer operators. Memory locations are
output by << in this example as hexadecimal (i.e., base-16) integers. The memory addresses
output by this program are platform dependent, so you may get different results when you run the
program. The address of a (line 11) and the value of aPtr (line 12) are identical in the output,
confirming that the address of a is indeed assigned to the pointer variable aPtr.
AN OUTCOMES-BASED APPROACH ON Data structures and Algorithm
2 hours lecture & 3 hours laboratory
DATA STRUCTURES AND ALGORITHM Version1
Output:
PASS-BY-REFERENCE WITH POINTERS
There are three ways in C++ to pass arguments to a function, pass-by-value, pass-by-
reference with reference arguments and pass-by-reference with pointer arguments.
You can use pointers and the indirection operator (*) to accomplish pass-by-reference
(exactly as pass-by-reference is done in C programs—C does not have references). When
calling a function with an argument that should be modified, the address of the argument is
passed. This is normally accomplished by applying the address operator (&) to the name of the
variable whose value will be modified.
The program below passes variable number by value (line 14) to function cubeByValue
(lines 19–22), which cubes its argument and passes the new value back to main using a return
statement (line 21). The new value is assigned to number (line 14) in main. The calling function
has the opportunity to examine the function call’s result before modifying variable number’s
value. For example, we could have stored the result of cubeByValue in another variable,
examined its value and assigned the result to number only after determining that the returned
value was reasonable.
AN OUTCOMES-BASED APPROACH ON Data structures and Algorithm
2 hours lecture & 3 hours laboratory
DATA STRUCTURES AND ALGORITHM Version1
Example 3: Pass-by-value used to cube a variable’s value.
Output:
AN OUTCOMES-BASED APPROACH ON Data structures and Algorithm
2 hours lecture & 3 hours laboratory
DATA STRUCTURES AND ALGORITHM Version1
Example 4: Pass-by-Reference with Pointers
Output:
A function receiving an address as an argument must define a pointer parameter to
receive the address. For example, the header for function cubeByReference (line 21) specifies
that cubeByReference receives the address of an int variable (i.e., a pointer to an int) as an
argument, stores the address in nPtr and does not return a value.
Function cubeByReference’s prototype (line 7) contains int * in parentheses. As with
other types, it isn’t necessary to include the names of pointer parameters in prototypes.
Parameter names included for documentation purposes are ignored by the compiler.
GRAPHICAL ANALYSIS OF PASS-BY-VALUE AND PASS-BY-REFERENCE
In the diagrams, the values in blue rectangles above a given expression or variable
represent the value of that expression or variable. Each diagram’s right column shows functions
cubeByValue (Example 3) and cubeByReference (Example 4) only when they’re executing.
AN OUTCOMES-BASED APPROACH ON Data structures and Algorithm
2 hours lecture & 3 hours laboratory
DATA STRUCTURES AND ALGORITHM Version1
AN OUTCOMES-BASED APPROACH ON Data structures and Algorithm
2 hours lecture & 3 hours laboratory
DATA STRUCTURES AND ALGORITHM Version1
Before main calls cubeByReference:
AN OUTCOMES-BASED APPROACH ON Data structures and Algorithm
2 hours lecture & 3 hours laboratory
DATA STRUCTURES AND ALGORITHM Version1
B. SUPPLEMENTARY MATERIALS
1. https://www.youtube.com/watch?v=8ViFuvTezIc
2. https://www.programiz.com/cpp-programming/pointers
3. https://www.tutorialspoint.com/cplusplus/cpp_references.htm
IV. LEARNING ACTIVITIES
A. Laboratory Work
U Understanding
AP Applying
ASSESSMENT
AN Analyzing
E Evaluating
C Creating