chapter 3
chapter 3
Collage of Informatics
Second year software engineering students
Course name: Fundamentals of Programming II
Course code :SEng2021
Chapter Three
Pointers
BY:Degaga .A
Topics we covered are:
BY:Degaga .A
Revision of variable
Variable have been explained as location in the computer
memory which can be accessed by their identifier.
it reserves memory block for the value.
The memory of your computer can be imagined as a
succession of memory cells.
Each cell can be easily located in the memory because it has
a unique address and all the memory cells follow a
successive pattern.
For example if we are looking for cell 1776, we know that it is going to be right
between cells 1775 and 1777
Basic concept of pointers
Output:
100 FFF4
56.47 FFF0
6
How to apply reference operator(&)
in pointer
Reference Operator allows you to obtain the memory address of a
variable, which can then be stored in a pointer.
So pointer is a variable that used to store memory address of a variable
accessed using (&) operator.
Example:
int Var = 42; / / Declare an integer variable
int* ptr = &Var; // Use & to get the address of Var
The symbol before ptr variable is known as The asterisk (*) or the
dereference operator.
In above example The asterisk (*) symbol describe ptr variable is
pointer variable.
Dereference operator.
if (ptr1 == ptr3) {
cout ("ptr1 and ptr3 point to the same location.\n"); // This will print
}
2. Inequality Operator (!=)
The inequality operator (!=) checks if two pointers do not point
to the same memory address.
Example:
int a = 10;
int b = 20;
int *ptr1 = &a; // Pointer to a
int *ptr2 = &b; // Pointer to b
if (ptr1 != ptr2) {
cout ("ptr1 and ptr2 point to different locations.\n"); // This will print
3.Less Than Operator (<)
The less than operator (<) compares two pointers and checks if the
address of the first pointer is less than the address of the second
pointer.
int arr[] = {10, 20, 30};
int *ptr1 = &arr[0]; // Points to the first element (10)
int *ptr2 = &arr[2]; // Points to the third element (30)
if (ptr1 < ptr2) {
cou("ptr1 points to an earlier element than ptr2.\n"); // This will
print
4.Greater Than Operator (>)
The greater than operator (>) compares two pointers and checks if the
address of the first pointer is greater than the address of the second
pointer.
int arr[] = {10, 20, 30};
int *ptr1 = &arr[2]; // Points to the third element (30)
int *ptr2 = &arr[0]; // Points to the first element (10)
#include <iostream>
using namespace std;
int increment( int * ptr)
{
*ptr=*ptr+2;
return *ptr;
}
int main()
{
int num=4;
int result=increment(&num);
cout<<result;
return 0; }
Advantages of using Pointers
• Pointers are variables which store the address of other variables in C++.
• More than one variable can be modified and returned by function
using pointers.
• Memory can be dynamically allocated and de-allocated using pointers.
• Efficient Passing of Arguments:
When passing large structures or arrays to functions, pointers enable
efficient argument passing by reference rather than copying the entire
data, reducing memory and time overhead.
• Data Structures Implementation:
Pointers are crucial for implementing complex data structures like linked
lists, trees and more.
Reading assignment
• How manipulate an string with pointer.
• Null pointer