04 Pointer
04 Pointer
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
C++
2
Outline
▪ What is pointer?
▪ Examples
3
Introduction
❑ Computer Memory
4
Introduction
❑ Computer Memory
▪ Each variable we create in the program has a location in the computer’s memory
▪ 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.
value
point to variable
name
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
15
Pointer and Array
❑ Remark
▪ 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
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++
#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
22