Data structure and
programming II
Pointer
Write program in C++ using pointer
Remark: Pointer is a variable that store address of
another variable of the same data type.
Lecture overview
❑ Overall lectures
1. Introduction to algorithm 7. Recursive
2. Basic data types and statements 8. File IO
9. Pointers
3. Control structures and Loop
10. Linked Lists
4. Array
11. Stacks and Queues
5. Data structure 12. Sorting algorithms
6. Sub-programs 13. Trees
C++
2
Outline
▪ What is pointer?
▪ What are the advantages of using pointer?
▪ How to use pointer
▪ Examples
3
Introduction
❑ Computer Memory
▪ To understand pointers, you should have knowledge about address in
computer memory
▪ A computer memory location has an address and holds a content (value)
▪ The address is a numerical number (expressed in hexadecimal)
▪ An integer value consumes 4 bytes of memory
4
Introduction
❑ Computer Memory
▪ Each variable we create in the program has a location in the computer’s memory
▪ The value of the variable is stored in the assigned location
▪ To know where the data of normal variable is stored, we use operator &
▪ & gives the address occupied by a variable
▪ Example:
▪ If num is a variable, then &num gives the address of that variable
5
Introduction
❑ What is pointer?
▪ A pointer is a variable that holds the memory address of another variable of the
same type.
▪ Pointers are used to access the memory address and values at that address.
pointer* variable address
value
point to variable
name
An example of a pointer variable
pointing to a normal variable
6
Pointer Vs. Normal variable
❑ Remark
▪ A normal variable is used to store value, while a pointer variable is used to
store address (reference) of another variable
▪ Pointers are representation of addresses
▪ We can have a pointer to any variable type
7
Advantages of using pointer?
❑ Some main advantages
1. Use less memory
▪ Dynamic memory allocation
2. Program runs faster
▪ Increase execution speed and reduce execution time
3. Efficient when work with array, structure, list, stack, queue, …
4. Provide another way to access array element
5. Instead of copying data, pointer just point to an existing data
6. A function can return more than one value by passing via
function argument
8
Pointer Operator
❑ What?
▪ Two operators when work with pointer
▪ Address operator (reference operator)
▪ It uses &
▪ It returns memory address
▪ Indirection operator (deference operator or value operator)
▪ It uses *
▪ It returns value
9
Pointer Declaration
❑ Syntax
10
Pointer Initialization
❑ Syntax
11
Access to Pointer Variable
❑ Syntax
12
Example 1
❑ Not using pointer
What are the values of a and b here? a is 1, b is 2 13
Example 2:
❑ Using pointer
What are the values of a and b here? a is 2, b is 1 14
Pointer and Array
15
Pointer and Array
❑ Remark
▪ Suppose we have variables
▪ Var arr[10]: integer
▪ Var *p:integer
▪ Array name arr represents the address of the first elements of this array (&arr[0])
▪ We can say
▪ p = arr; // p point to the first element (arr[0]) in the array
▪ When a pointer points to an array, the value of the pointer is the first array element
▪ write(*p) 16
NOTE
❑ Reference (&) Vs. Deference (*) operator
▪ &: to get address of any variable
▪ *: to get value at the address that the point stores
Example:
▪ If an integer variable, say n, is stored in memory address 0xf1bd23,
and n contains a value of 5.
Then:
Reference operator &n gives the value of 0xf1bd23
Deference operator *n gives the value of 5
17
Q&A
18
Example 1: Using C++
▪ Suppose we have program as follows
#include<iostream>
using namespace std;
int main(){
int num=10;
int *ptr;
ptr = #
cout<<“num=”<<num<<endl;
cout<<“Address of variable num is: ”<<&num<<endl;
cout<<“Value in variable pointer ptr is: ”<<ptr<<endl;
cout<<“*ptr=”<<*ptr<<endl; //Value in that address
} 19
Example 2: Using C++
#include<iostream>
▪ Suppose we have program as follow
using namespace std; Output
int main(){
int *pc, c;
c=5;
cout<<“Address of c: ”<<&c<<endl;
cout<<“Value of c: ”<<c<<endl;
pc = &c;
cout<<“Address that pc holds: ”<<pc<<endl;
cout<<“Value of address that pc holds: ”<<*pc<<endl;
c = 11;
cout<<“Address that pc holds: ”<<pc<<endl;
cout<<“Value of address that pc holds: ”<<*pc<<endl;
*pc = 2;
cout<<“Address of c: ”<<&c<<endl;
cout<<“Value of c: ”<<c<<endl;
}
20
Pointer Examples
Write program in C++ using pointer
Remark: Pointer is a variable that store address of
another variable of the same data type.
Q&A
22