0% found this document useful (0 votes)
3 views

chapter 3

This document is a comprehensive guide on pointers in programming, covering their basic concepts, declaration, initialization, and operations such as dereferencing and pointer arithmetic. It explains the relationship between pointers and arrays, strings, and function calling by reference, highlighting the advantages of using pointers for efficient memory management and data manipulation. The document includes code examples and practical applications of pointers in C++.

Uploaded by

bilisumat03
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

chapter 3

This document is a comprehensive guide on pointers in programming, covering their basic concepts, declaration, initialization, and operations such as dereferencing and pointer arithmetic. It explains the relationship between pointers and arrays, strings, and function calling by reference, highlighting the advantages of using pointers for efficient memory management and data manipulation. The document includes code examples and practical applications of pointers in C++.

Uploaded by

bilisumat03
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Bule Hora University

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:

 Basic concept of pointers


 Pointer variable declaration and Initialization
 Pointer operation(reference, dereference and arithmetic
operation)
 pointers and arrays
 Strings and pointers
 Revisiting function calling by reference (using pointers)
 Why pointer?

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

A pointer is a variable that stores address of other variable.

 It used to manipulate data stored in other variables or It

provides an indirect way of accessing data in memory.

 addresses represented in hexadecimal form.

 Every variable is a memory location and every memory location has

its address defined which can be accessed using ampersand &

operator which denotes an address in memory.


Address Operator Or Reference Operator(&)
The address operator, represented by the ampersand
symbol (&).
 it used to retrieves the memory address of a variable. Because
that address operator is crucial for working with pointers, as it
allows programmers to obtain the location in memory where a
variable is stored.
 The following example shows how to access the address of
variable using Address Operator ((&)
Example
#include <iostream.h> FFF0 56.47
valu
e FFF1
void main( )
{ FFF2
int data = 100; FFF3
float value = 56.47; data FFF4 100
cout << data << &data << endl;
cout << value << &value << endl; FFF5
} FFF6

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.

The * operator, when used with pointers, either declares a pointer or


dereferences the pointer’s value. The dereference operator can be
literally translated to "value pointed by”.
Example:
 To declare pointer:-
int *ptr; // here * is used declare pointer ptr.
 dereferences the pointer’s value:-
int value=*ptr;
Pointer Declaration
General syntax Pointer declaration:
dataType *pointerVarName;
 The pointer variable pointerVarName is used to point to a
value of type datatype
 The * before the pointerVarName indicates that this is a
pointer variable
Example:
int *ptr1;
float *ptr2;
 ptr1 is a pointer to an integer data and ptr2 is a
pointer to a float data type.
 Int *ptr1,*ptr ; //for multiple pointer.
 Note: The data type of the pointer must match the data type of the
variable it points to.
 For example, an int* should point to an int, and a float* should
point to a float.
 Mismatched types can lead to compilation errors or undefined
behavior.
Pointer Initialization
 Pointer initialization is the process of assigning a valid memory address to a
pointer variable.
 is typically done using the address-of operator (&), which retrieves the memory
address of a variable.
 The general syntax for declaring and initializing a pointer is as follows:

data_type *pointer_name = &variable_name;


int num = 42; // Declare an integer variable
int *ptr = &num;
Note: If a pointer is declared but not initialized, it contains a garbage value, which
can lead to undefined behavior if dereferenced. It’s good practice to initialize pointers
to NULL if they are not immediately assigned an address:
example int *ptr=null;
Accessing value of variable pointed Pointer

 Accessing pointers involves using the dereferencing operator (*) to


retrieve or modify the value stored at the memory address that the
pointer references.
Example:
int num = 10; // Declare an integer variable
int *ptr = &num; //intialiazation.
*ptr; // accessing the value of num

 Example of how to declare, initialize and accessing value pointed


to pointer
 & is reference operator and read as “address of”.
 * is dereference operator and read as “value pointed by”.
// sample codes
}
#include<iostream>
using namespace std;
int main()
{
int x=5,y=10;
int *p1,*p2;
p1=&x; assigning the address of the variables to the pointer.
p2=&y;
When we add * symbol in front of the pointer variable we access the values of
variable pointed to the pointer.
Example :*p1.
also it possible to modify the value of variable assigned to pointer. Example:
*p1=10;// moifying the values of variable assigned to p1.
*p2=*p1; // the value of p1 is assigned into p2.
p1=p2; //the address of p1 is equal with p2
*p1=20;
cout<<x<<endl<<y; // it display x=10 and y=20
Pointer arithmetic
 It applies or valid to any contiguous block of memory.
 so it useful when working with arrays and dynamic memory.

Arithmetic operators that can be used on pointers are increment,


decrement ,addition, subtraction, difference and comparison operation.
1.Incrementing and Decrementing Pointers(++, --)
When you increment or decrement a pointer, you are moving the
pointer to the next or previous memory location based on the size of
the data type it points to.
.
int tempp [5]={4,5,6,8,9};
int *temptr=tempp;
in this case, temptr //hold the address 4 or the address of the array first
element.
*temptr //hold the value of 4 or the value of the first array element
temptr++; //hold the address of the next array integer which 5.
* temptr++ //hold the value of the next array integer which 5.

temptr--; //hold the address of the first element which is 4.


* temptr--; //hold the value of the first element which is 4.
2. Adding and Subtracting Integers to Pointers ( - ,+)
 You can add or subtract an integer value from a pointer.
 it used moves forward and backward n elements of data type
Example:-
temptr +1 // hold the address of 5 or second element.
*temptr +1 // hold the value of 5 or second element

temptr -1 //hold the address of 4 or first element.


*temptr -1 //hold the value of 4 or first element
3.Pointer Difference
Subtracting one pointer from another gives the
number of elements (not bytes) between them.
int *start = &arr[0];
int *end = &arr[2];
int diff = end - start; //results 2. So it calculates the index
difference

But int diff= *end-*start //calculates value difference


4.Comparison (==, !=, <, >):
Pointers can be compared to check their relative positions in memory.
1. Equality Operator (==)
The equality operator (==) checks if two pointers 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
int *ptr3 = &a; // Another pointer to a

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)

if (ptr1 > ptr2) {


cout ("ptr1 points to a later element than ptr2.\n"); // This will
print
}
pointers and arrays
 When assign an array to the pointer it assigned the to the address of
first element.
 array elements can be accessed by using either pointer arithmetic or
array-style indexing
 example:
int num[5]={1,2,3.4,5};
int *p;
p=num; // pointed to address of array element 1
but p++; point to address of array element 2.
*(p+1) used access element 2
Strings and pointers
 When an string are assigned to the pointer variable the pointer points to the firs
character.
Example
char str[]=“hello”;
Here, char *ptr = str;
ptr points to the first character which h. So *ptr access h;
but to access the address of each character we must use this syntax: (void*) pointer
name. example to access address of h we must write like this:(void*)ptr;
ptr++;// it points to the next character
void*)ptr; //access address of e
*ptr; //this pointer access e;
Function calling by reference (using pointers)
In C++, you can pass arguments to functions by reference using pointers.
This allows a function add two number on the arguments' values directly rather than
working on copies.

#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

You might also like