You are on page 1of 11

Republic of the Philippines

NUEVA VIZCAYA STATE UNIVERSITY


Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2021-2022

COLLEGE OF ARTS AND SCIENCES


Bayombong Campus

DEGREE PROGRAM BSIT COURSE NO. IT ECC 4


SPECIALIZATION Web Dev and COURSE TITLE Data Structure and Algorithm
Network Design
and Management
YEAR LEVEL 1st Year TIME FRAME 5 WK NO. 6-8 IM NO. 04
hours

I. CHAPTER IV – POINTERS (Concept only)

II. LESSON TITLE


A. The Basic of Pointers no Pointers in Java
B. Pointers to Pointers
C. User-Defined Data Type and Classes

III. LESSON OVERVIEW


This lesson will give the students the knowledge on how to declare pointer in C++ program, simulate by
tracing elements and memory addresses stored in memory and to use the concepts of pointers and
pointer to pointer in creating C++ program.

IV. DESIRED LEARNING OUTCOMES

At the end of the lesson, the student should be able to:

1. To declare a pointer (no pointers in java only in C++)


2. To simulate by tracing the elements and memory addresses stored in the memory.
3. To apply the concepts of pointers.

V. LESSON CONTENT

CHAPTER IV: POINTERS


Whenever you reference the name of a variable, the name of an element of a structure, or the
name of an attribute of a class, you are telling the computer that you want to do something with the
contents stored at the corresponding memory location.
char grade = 'A';
char oldGrade;
oldGrade = grade;
 In the first statement in the following example, the computer is told to store the letter A into the memory
location represented by the variable grade.
 The last statement tells the computer to copy the contents of the memory location represented by the
grade variable and store it in the memory location represented by the oldGrade variable.

A. What is a Pointer?

 it is a variable, which stores the address of another variable


 it references a memory location that contains a memory address

“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 1 of 11
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2021-2022

 it can be used as an element of a structure and as an attribute of a class in some programming


languages such as C++, but not in Java

B. Declaring a Pointer

There are four parts of this declaration:


 data type
o the data type of the memory address stored in the pointer
 asterisk (*)
o tells the computer that you are declaring a pointer
 variable name
o the name that uniquely identifies the pointer and is used to reference the pointer within
your program
 Semicolon
o tells the computer this is an instruction (statement)

Here is an example of declaring a pointer variable called ptGrade:

char *ptGrade;

C. Data Type and Pointers


o a data type tells the computer the amount of memory to reserve and the kind of data that will be
stored at that memory location
o however, the data type of a pointer tells the computer the data type of the value at the memory
location whose address is contained in the pointer

D. The Basic of Pointers


o the asterisk (*) used when declaring a pointer tells the computer the amount of memory to reserve
and the kind of data that will be stored at that location
 that is, the memory size is sufficient to hold a memory address, and the kind of data stored
there is a memory address

Consider the following example which declares four variables:

int studentNumber;
char grade;
char *ptGrade;
int *ptStudentNumber;

 the first statement declares an integer variable called studentNumber


 the second statement declares a char variable called grade
 The last two statements each declare a pointer

Figure 2-3 depicts memory reserved by these statements. Assume that a memory address is 4 bytes
for this example.

“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 2 of 11
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2021-2022

Figure 2-3: Memory allocated when two pointers and two variables are declared
o The char data type in the ptGrade pointer declaration tells the computer that the address that will be
stored in ptGrade is the address of a character.
o Likewise, the int data type of the ptStudentNumber pointer states that the contents of the memory
location associated with ptStudentNumber will contain the address of an integer variable, which will
be the address of the studentNumber variable.

E. Assigning an Address to a Pointer


o address operator (&)
 used to assigned the address of a variable to a pointer variable

Consider the following examples:


Example 1:
oldGrade = grade;

o the assignment statement tells the computer to copy the value stored at the memory location that is
associated with the grade variable and store the value into the memory location associated with the
oldGrade variable:
 An assignment statement implies that you want the contents of a variable and not the
address of the variable.
o On the other hand, the address operator tells the computer to ignore the implied assignment and
assign the memory address of the variable and not the content of the variable.

“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 3 of 11
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2021-2022

Example 2:
int studentNumber;
char grade;
char *ptGrade;
int *ptStudentNumber;

ptGrade = &grade;
ptStudentNumber = &studentNumber;

o The next example tells the computer by using the address operator to copy the address of the variable
to the pointer variable.
 That is, the memory address of the grade variable is copied to the ptGrade pointer variable,
and the memory address of the studentNumber variable is assigned to the ptStudentNumber
pointer:

Figure 2-4: Memory allocated after pointers are assigned memory addresses

F. Accessing Data Pointed to by a Pointer


o Remember that the assignment statement tells the computer to copy the contents of a variable
regardless if the content is a memory address or any other value.

char oldGrade;
char grade = 'A';
char *ptGrade;
char *ptOldGrade;
ptGrade = &grade;

 The first two statements declare variables, one of which is initialized with a value. The next
two statements declare pointer variables.
 And the last statement assigns the address of the first variable to pointer variables.

“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 4 of 11
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2021-2022

Figure 2-5: Memory allocated after values are assigned to variables


Situation 1:
Let’s say a programmer wants to use the value stored in the grade variable to display the grade on the
screen. However, the programmer wants to use only the ptGrade pointer to do this. Here’s how it is done.
Solution:
Pointer dereferencing operator (sometimes called the dereferencing operator), which is the asterisk
(*)
 used to access the value of variables pointed by the pointer variables
 Think of dereferencing as telling the computer you are referring to go to the memory address
contained in the pointer and then perform the operation. Without dereferencing, the computer is
told to use the contents of the pointer when performing the operation.

Let’s say that you want to copy the content of ptGrade to ptOldGrade. Here’s how you would do it:

ptOldGrade = ptGrade;

Figure 2-6: Memory allocated after the value of the ptGrade is copied to ptOldGrade.
Situation 2:
Now let’s suppose you want to copy the contents of grade to the oldGrade variable, but you only want to
use the ptGrade pointer.
Solution:
You do this by dereferencing the ptGrade pointer variable using the asterisk (*) as the dereferencing
pointer operator as shown here:
char oldGrade = *ptGrade;
The previous statement tells the computer to go to the memory address contained in the ptGrade pointer
variable and then perform the assignment operation, which copies the value of memory address 2 to the
memory address represented by the oldGrade variable, which is memory address 1.

“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 5 of 11
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2021-2022

Figure 2-7: Memory allocated after the contents of the memory address pointed to by ptGrade is
copied to the oldGrade variable. 3
The general syntax of the Dereference operator is as follows:
*pointer_variable
In this example, pointer variable denotes the variable defined as pointer. The * placed before the
pointer_variable denotes the value pointed by the pointer_variable.

For example:
exforsys = 100;
test = exforsys;
x = &exforsys;
y=*x;
cout<<”The value of y: “<<y;

Output:
The value of y: 100
In the above example, the assignment takes place as below:

That is

Pointers to Pointers
Imagine having a list of a million students along with their final grades and student numbers and
being asked to sort the list by last name, first name, and student number. Intuitively, you might think about
making two copies of this list, each placed in one of the sort orders. However, this wastes memory. There
is a better approach to sort the list: use pointers to pointers.

“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 6 of 11
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2021-2022

 is also a variable that contains the memory address, except a pointer to a pointer contains the memory
address of another pointer variable
 a pointer to a pointer is declared using a double asterisk (**)

Here is an example of declaring a pointer variable called ptPtGrade:


char **ptPtGrade;
Sample:
char inital1 = 'D', inital2 = 'A', inital3 = 'C', inital4 = 'B';
char *ptInitial, **ptPtInitial;
ptInitial = &inital1;
ptPtInitial = &ptInitial;
With variables declared, the next two statements assign values to the pointer and to the pointer to a
pointer. In both cases, the ampersand (&) is used as the dereferencing operator.
The ptInitial pointer variable is assigned the address of variable inital1, which is memory address
1. The ptPtInitial pointer to a pointer variable is assigned the memory address of ptInitial. The address of
ptInitial is memory address 5. Figure 2-10 shows the allocated memory after these statements execute.

Figure 2-10: The pointer to a pointer variable is assigned the


memory address of the ptInitial pointer.
Programmers use a pointer to a pointer to tell a computer to use the contents of the memory
address contained in the pointer variable that the pointer to a pointer is pointing to. This is a mouthful, so
we’ll restate this using an example:
You can use the content of the inital1 variable by referencing the ptPtInitial variable. Here’s how
this is done:
cout << **ptPtInitial;

The cout statement is used in C++ to display something on the screen. In this example, you’re
displaying the content of the initial1 variable, although it doesn’t seem to be doing so. This statement is
telling the computer to go to the memory address stored in the ptPtInitial pointer to a pointer variable,
which is memory address 5 (see Figure 2-11).
The content of that memory address is another memory address, which is memory address 1.
The computer is told to go to memory address 1 and display the content of that memory address, which
is the letter D.

“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 7 of 11
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2021-2022

Figure 2-11: Two memory addresses are referenced


when using a pointer to a pointer to display a value on the screen.
Two types of Pointer

1. Pointer to Void

General Syntax:
void* pointer_variable;
 void is used as a keyword.

Referring back to pointer definitions and usage, it is known that the data type the pointer variable
defines is the same as the data type the pointer points to. The address placed in a pointer must have the
same type as the pointer.
For example:

Is correct because the address of integer variable is stored in an


int i; integer pointer.
float f;
int* exf; If a user writes the statement:
float* test;
then
exf=&i; exf=&f;

Then this statement produces an error. The address of the float


variable is stored in an integer pointer that is incorrect.

Similarly, if the programmer tries to place the address of an integer variable to a float pointer, such as:
test=&i;
The above statement will also show an error.

1. Pointer to Void
 is a special type of pointer that the programmer can use to point to any data type.
Using the above example, the programmer declares pointer to void in this manner:
“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 8 of 11
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2021-2022

void* sample;
 Using the above example’s definition and assigning the pointer to void to the address of an
integer variable is perfectly correct.

sample=&i;
 Using the above example to define the pointer to void and assign the pointer to void to the
address of a float variable as below is also perfectly correct.

sample=&f;
Pointer to void, or a void pointer, is a special type of pointer that has a great facility of pointing to any
data type. There are limitations in the usage of void pointers that are explained below.
The concept of dereferencing using the operator * has been explained in an earlier. The programmer
must note that void pointers cannot be de-referenced in the same manner. Direct dereferencing of void
pointer is not permitted. The programmer must change the pointer to void as any other pointer type that
points to valid data types such as, int, char, float and then dereference it. This conversion of pointer to
some other valid data type is achieved by using the concept of type-casting (refer to type-casting section
of this tutorial).
2. NULL Pointer
 is a type of pointer of any data type and generally takes a value as zero
 this denotes that NULL pointer does not point to any valid memory address
For example:
int* exforsys;
exforsys=0;
The above statement denotes exforsys as an integer pointer type that does not point to a valid
memory address. This shows that exforsys has a NULL pointer value.
The difference between void pointers and NULL pointers:
 A Void pointer is a special type of pointer of void and denotes that it can point to any data type.
 NULL pointers can take any pointer type, but do not point to any valid reference or memory address. It
is important to note that a NULL pointer is different from a pointer that is not initialized.

For example, if a programmer uses the program below:


#include <iostream.h>
int *exforsys;
void main()
{
*exforsys=100;
}

The output of the above program is none.


NULL POINTER ASSIGNMENT
The above program will result in a runtime error. This means that the pointer variable exforsys is
not assigned any valid address and, therefore, attempting to access the address 0 gives the above error
message.fo

Sample program for pointer: Applicable for BS Computer Science Student


“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 9 of 11
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2021-2022

#include<iostream>
using namespace std;
int main()
{
/*
instruction: initialized 10 variables and 10 pointers
p1- > p9 -> v10
p3->p5 ->p4->v3
p10 ->p6 -> v8
p7 -> p8 ->p2 ->v1

Take note the number of * depends on how many times u used the pointer to point a certain
variable
example p7; p7 is used 3 times to point in variable v1

*/
//declaration
char v1='e', v2='j', v3='l', v4='c', v5='s', v6='m', v7='y', v8='v', v9='r', v10='o';
char **p1, *p2, ***p3,*p4,**p5,*p6, ***p7,**p8,*p9,**p10;

// p1 is pointing to the address of p9; p9 is pointing to the address of variable 10


p1=&p9;
p9=&v10;

// p3 is pointing to the address of p5, p5 is pointing to the address of p4, p4 is pointing to the address of
var 3
p3=&p5;
p5=&p4;
p4=&v3;

// p1 is pointing to the address of p10; p6 is pointing to the address of variable 8


p10=&p6;
p6=&v8;

// p7 is pointing to the address of p8, p8 is pointing to the address of p2, p2 is pointing to the address of
var 1
p7=&p8;

“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 10 of 11
Republic of the Philippines
NUEVA VIZCAYA STATE UNIVERSITY
Bayombong, Nueva Vizcaya
INSTRUCTIONAL MODULE
IM No.:IM-IT ECC4-2NDSEM-2021-2022

p8=&p2;
p2=&v1;

//dislay the output

cout<<*P4<<*P9<<*P6<<*P2;
}

IX. REFERENCES

Books:
1. Malik, D. S. (2013). C programming: program design including data structures. Boston, Mass:
Course Technology, Cengage Learning.
2. Dale, N., Joyce, D. T., & Weems, C. (2012). Object-Oriented Data Structures Using Java (3rd
Ed.). Jones & Bartlet Learning.Canada.
3. Drozdek, Adam, Data Structure and Algorithm in java, 2nd ed, Brooks/Cole Pub
4. Sahni, Sartaj, Data Structure, algorithm, and application in java, McGraw Hill
5. Preiss, Bruno, Data Structure and algorithms with object-oriented design pattern in java, Wiley
6. Data Structures Demystified, James Keogh & Ken Davidson, Copyright © 2004 by The
McGraw-Hill Companies

On-Line Resources:

1. cs-www.cs.yale.edu/homes/aspnes/classes/223/notes.pdf.
2. http://www.cs.tut.fi/~uds/material/uds_slides_4.pdf
3. http://en.wikipedia.org/wiki/Data_structure
4. http://freevideolectures.com/Course/2279/Data-Structures-And-Algorithms
5. http://courses.cs.vt.edu/csonline/DataStructures/Lessons/Introduction/index.html
6. http://www.tutorialspoint.com/java/java_data_structures.htm
7. http://www.cs.sunysb.edu/~skiena/214/lectures/
8. http://www.cs.utexas.edu/~novak/cs315contents.html
9. http://www.csd.uwo.ca/courses/CS2210b/

“In accordance with Section 185, Fair Use of Copyrighted Work of Republic Act 8293, the copyrighted works included in this material may be reproduced for educational
purposes only and not for commercial distribution,”
NVSU-FR-ICD-05-00 (081220) Page 11 of 11

You might also like