Faculty of Computer Science
Object Oriented Programming Language
Lecturer: AbdulBasir Momand
OOP Chapter 02
Chapter 2
Pointer Variable
Learning outcomes Chapter 02
Introduction to Pointer
What is pointer?
Pointer declaration and initialization
Pointer and function
Call by value
Call by reference
Pointer and array
Introduction to Pointer Chapter 02
• When writing a program, you declare the necessary variables and the
computer reserve a set amount of space in its memory for that particular
variable, and uses the variable's name to refer to that memory space.
• This will allow you to store something, namely the value of that variable, in
that space.
• Therefore, everything you declare has an address, just like the address of
your house.
• You can find out address of a particular variable, using pointer.
What is Pointer? Chapter 02
•Pointer is a variable in C++ that holds is the address of another variable.
•They have data type just like variables, an integer type pointer can hold
the address of an integer variable and an character type pointer can hold
the address of char variable.
Symbols Used in Pointer Chapter 02
What is Security?
Symbol Name Description
& (ampersand) Address operator Determine the address of a variable.
∗ (asterisk) Indirection operator Access the value of an address.
Address Operator (&) Chapter 02
& (ampersand) is known as address operator.
The & address operator is used to give the address occupied by a
variable.
To know where the data is stored, address operator (&) is used.
Each variable you create in your program is assigned a location in the
computer's memory. The value of the variable is stored in the location
assigned.
Example:
If var is a variable then, &var gives the address of that variable.
Declaring a pointer Chapter 02
What is Security?
The pointer in C++ language can be declared using ∗ (asterisk symbol).
Syntax:
Type *ptr-var-name ;
Type* ptr-var-name ;
Example:
int∗ a; //pointer to int
char ∗c; //pointer to char
Pointer Initialization Chapter 02
The pointer in C++ language can be initialized using & (ampersand).
Syntax of pointer Initialization:
type *ptr-var-name = & variable-name
Example:
int a = 10;
int *ptr; //pointer variable declaration
ptr = &a; //pointer variable Initialization
Example 1 Chapter 02
What is Security?
#include <iostream>
using namespace std;
int main()
{
int var1 = 3;
int var2 = 24;
cout << "Address of var1: "<< &var1 << endl;
cout << "Address of var2: " << &var2 << endl;
}
Example 2 Chapter 02
What is Security?
#include <iostream>
using namespace std;
int main()
{
int a = 25;
int* p; // pointer declaration
p = &a // pointer initialization
cout << “the address of variable a is “ << &a << endl;
cout << “the value of p pointer varaible is “ << p << endl;
cout << “the value of p variable is “ << *p << endl;
cout << “address of p vaiable is“ << & p;
}
Class Activity Chapter 02
What is Security?
• Write a program using pointer to swap to numbers without
using third variables?
Example 3 Chapter 02
What is Security?
// program to swap two number using pointer
#include <iostream>
using namespace std;
int main(){
int a= 10;
int b=20;
int *p1 = &a;
int *p2 = &b;
cout<<"before swap a= “ << *p1 << " and b= "<< *p2 <<endl;
*p1= *p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
cout<<"after swap a = "<<*p1<<"and b= "<<*p2;
}
OOP Chapter 02
What is Security?
Pointer and Function
Passing arguments to function Chapter 02
What is Security?
There are two ways to pass value or data to function:
1. Call by value
2. Call by reference
Passing arguments to function Chapter 02
What is Security?
1. Call by value:
In call by value, original value is not modified.
In call by value, value being passed to the function is locally stored by the
function parameter.
If you change the value of function parameter, it is changed for the
current function only. It will not change the value of variable inside the
caller method such as main().
Call by value example 1 Chapter 02
What is Security?
#include <iostream> void change(int data)
using namespace std; {
void change(int data); data = 5;
int main() }
{ Output: Value of the data is: 3
int data = 3;
change(data);
cout << "Value of the data is: " << data<< endl;
return 0;
}
Call by value example 2 Chapter 02
What is Security?
#include <iostream> cout << "After swap, value of b :" << b;
using namespace std; }
void swap(int x, int y); void swap(int x, int y)
int main () {
{ int temp;
int a = 100; temp = x; // save the value of x
int b = 200; x = y; // put y into x
cout << "Before swap, value of a :" << a; y = temp; // put x into y
cout << "Before swap, value of b :" << b; }
// calling a function to swap the values.
swap(a, b);
cout << "After swap, value of a :" << a;
Call by value example 2 Chapter 02
What is Security?
Output:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200
Passing arguments to function Chapter 02
What is Security?
2. Call by reference:
In call by reference, original value is modified because we pass reference
(address).
Here, address of the value is passed in the function, so actual and formal
arguments share the same address space.
Hence, value changed inside the function, is reflected inside as well as
outside the function.
Call by reference example Chapter 02
What is Security?
#include<iostream> int main()
using namespace std; {
void swap(int *x, int *y) int x = 500, y = 100;
{ swap(&x, &y);
int swap; cout<< "Value of x is: “ <<x;
swap=*x; cout<<"Value of y is: "<<y;
*x=*y; return 0;
*y=swap; }
}
Pointers and Arrays Chapter 02
What is Security?
In C++, Pointers are variables that hold addresses of other variables.
Pointers not only can store the address of a single variable, it can also
store the address of cells of an array.
We can use a pointer to point to an array, and then we can use that
pointer to access the array elements.
Pointers and Arrays Chapter 02
What is Security?
Simple example:
int *ptr;
int arr[5];
ptr = &arr[2];
// &arr[2] is the address of third element of a[5].
Note: The code ptr = arr; stores the address of the first element of the array in variable
ptr.
Point to Every Array Elements Chapter 02
What is Security?
If you want to point to the fourth element of the array using the same pointer ptr, we
can use the following structure.
int *ptr;
int arr[5];
ptr = arr;
ptr + 1 //is equivalent to &arr[1];
ptr + 2 //is equivalent to &arr[2];
ptr + 3 //is equivalent to &arr[3];
ptr + 4 //is equivalent to &arr[4];
Example program Chapter 02
What is Security?
// C++ Program to display address of each for (int i = 0; i < 3; i++)
element of an array {
#include <iostream> cout << "ptr + " << i << " = "<< ptr + i ;
using namespace std; }
int main() }
{
int arr[3];
int *ptr;
ptr = arr;
cout<<"Displaying address using pointers:
";
Practice Chapter 02
What is Security?
• Write a c++ program to create an array of string and display the element
of array and address of array element using pointer?
Example Chapter 02
What is Security?
#include <iostream> operator: ";
using namespace std; for(int i = 0; i<4; i++)
int main(){ {
string a[4] = {"c", "c++", "java", "python"}; cout<<"array ["<<i<<"] = "<< &a[i];
string* ptr; }
ptr = a; //address using pointer
//array elements cout<<"address of array element using pointer:
cout<<"array elements are: "; ";
for (int i = 0 ; i <4; i++) { for(int i = 0; i<4; i++)
cout<<a[i]<<endl; {
} cout<<"ptr +"<<i<<" = "<<ptr+i;
cout<<"address of array element using address } }
OOP Chapter 02
What is Security?
End of Chapter