Professional Documents
Culture Documents
V. LESSON CONTENT
A. What is a Pointer?
“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
B. Declaring a Pointer
char *ptGrade;
int studentNumber;
char grade;
char *ptGrade;
int *ptStudentNumber;
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.
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
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
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 (**)
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
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:
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.
#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;
// 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;
// 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;
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