0% found this document useful (0 votes)
11 views17 pages

Pointers Arithmetic

Uploaded by

alihamza4851
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views17 pages

Pointers Arithmetic

Uploaded by

alihamza4851
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Pointers

Arithmetic
PF TEAM, FIT UCP

PAGE
Topics Heading

 Variables and Memory Lecture


Allocations
 Introduction to Pointers
Outline
 Pointers Arithmetic
 Pointers VS Arrays

PAGE
Variables and Memory

 A computer memory location has an address and a data content


associated to that address
 Each variable that you have created in your program is assigned a
memory space (in term of bytes)
 The value of that variable is actually stored in those particular bytes
of computer’s memory

Note: Computer’s memory is byte addressable i.e. every byte has an address
PAGE
Variables and Memory

 In case of int num = 10;4-bytes are allocatedMemory


to
store the value/data of int type variable num at Address
a particularData
memory location 0000
 Hypothetically saying, int num; is assigned a memory
location having address 5000 4999
 The next free memory location will be at 5004,integer
5000 10
datatype is of 4-bytes 5004 Free space

9999
PAGE
Pointers
Pointer’s datatype is designed to hold the memory address
With pointers you can indirectly manipulate the content of other
variable
The pointer’s datatype and variable datatype should be same i.e.
integer pointer can not point to a character variable or vice versa

Right Wrong

int a = 10; int a = 10;


int *ptr; char *ptr;
/*ptr is a pointer to an int*/ /*ptr is a pointer to a char*/
ptr = &a; ptr = &a;
PAGE
Pointers
#include <iostream> Memory
using
 namespace std; Variable Address Value/
Data num
int main(){ 5000 10
num 5000 10
int num = 10;
int *ptr;
ptr = &a;
ptr 6400 5000
return 0;
} ptr *ptr
5000
Note: Memory address assigned to variable “a” and pointer “ptr”are
hypothetical PAGE
Pointers

 If a pointer is pointing to a variable, it can access the value of that variable as well by
using de-reference operator i.e. asterisk *
Code Output on Console

int a = 10; ptr value = 10


int *ptr; a = 20
/*address of a assigned to ptr */
ptr = &a;
Cout<<”ptr value = ”<<*ptr<<endl;
*ptr = 20;
cout<<”a = ”<<a<<endl;

Note: Changing value of “ptr” reflects in “a” and vice versa


PAGE
Pointers Arithmetic
 Some mathematical operations can be performed on pointers
 increment ++, decrement – – , addition +, subtraction –
 The arithmetic of pointers is NOT same as of other variables
Effect of arithmetic on different datatype
OPERATION char *prt int *ptr double *ptr
Bytes allocated sizeof(char) sizeof(int) sizeof(double)
to datatype 1-bytes 4-bytes 8-bytes
ptr++; Ptr = ptr + 1 Ptr = ptr + 4 Ptr = ptr + 8
ptr--; Ptr = ptr - 1 Ptr = ptr - 4 Ptr = ptr - 8
ptr = ptr + n Ptr = ptr + n*1 Ptr = ptr + n*4 Ptr = ptr + n*8
ptr = ptr - n Ptr = ptr - n*1 Ptr = ptr - n*4 Ptr = ptr - n*8
Generic Form ptr = ptr (+ or – ) n*sizeof(datatype)

Note: Number added or subtracted from the pointer’s value solely depends upon the datatype of pointer
PAGE
Variablesand
Pointers andArrays
Memory

th
 Name of the array is the address of 0 index of array
 Array name can be used as constant pointer, and pointers can be
used as array names
 Pointer arithmetic is very important while manipulation array

PAGE
Use of Array as Pointer
int arr[6] = {10, 20, 30, 40, 50, 60}

int *ptr = arr; //ptr pointing to arr

Following Table shows array and pointer manipulation

Address of Accessing Array Pointer to De-referencing


the Array Element Array pointer to array
arr 5000 arr[0] 10 ptr 5000 *(ptr) 10
arr + 1 5004 arr[1] 20 ptr + 1 5004 *(ptr + 1) 20
arr + 2 5008 arr[2] 30 ptr + 2 5008 *(ptr + 2) 30
arr + 3 5012 arr[3] 40 ptr + 3 5012 *(ptr + 3) 40
arr + 4 5016 arr[4] 50 ptr + 4 5016 *(ptr + 4) 50
arr + 5 5020 arr[5] 60 ptr + 5 5020 *(ptr + 5) 60

Note: *(arr+1), arr[1] and *(ptr + 1) are same PAGE


Variablesand
Pointers andArrays
Memory

 ptr = ptr + 1; switches the pointer to the next index of array


 ptr = ptr + 1; it will add 4 rather then 1 in final value of
pointer

Note: Pointer’s arithmetic is different from the arithmetic performed on


other data types
PAGE
Variablesand
Pointers andArrays
Memory

 Two pointers pointing at different indices of array arr


arr[0] arr[1] arr[2] arr[3] arr[4] arr[5]
10 20 30 40 50 60
int arr[6] = {10,20,30,40,50,60}
int *ptr1, ptr2;
ptr1 = arr;// ptr1 = &arr[0]
ptr2 = &arr[3]
ptr1 ptr2

PAGE
int arr[6] = {10,20,30,40,50,60}
VariablesHitch
Pointers and Memory
Hikes int *ptr;
ptr = arr; // ptr1 = &arr[0]

ptr pointing to index zero of arr



ptr Value of pointer i.e. address

*ptr Dereference the pointer, content of memory i.e. value

*++ptr//equivalent of *(++ptr) st
1 increment ptr then dereference the pointer

++*ptr //equivalent of ++(*ptr) 1st dereference the pointer then increment

*ptr++ //equivalent of (*ptr)++ 1st dereference the pointer then increment

Precedence principle; from right to left.


Postfix mode; increment happen after the expression
PAGE
Variables Code
Example and Memory
#include <iostream>
using namespace std;
Program output
value of arr = 005DF920
int main(){
• value of ptr = 005DF920
value of ++*ptr = 11
int arr[6] = {10,20,30,40,50,60}
value of *++ptr = 20
int *ptr1 = arr;// ptr1 = &arr[0]
value of *ptr++ = 20
cout<<”value of arr = ”<<arr<<endl; value of *ptr = 30
cout<<”value of ptr = ”<<ptr<<endl;
cout<<”value of ++*ptr = ”<<++*ptr<<endl;
cout<<”value of *++ptr = ”<<*++ptr<<endl;
Note:
cout<<”value of *ptr++ = ”<<*ptr++<<endl; 005DF920 is the address where
cout<<”value of *ptr = ”<<*ptr<<endl; memory is assigned to array.
You may have some other
return 0; address value
}
PAGE
Variablesand
Pointers andArrays
Memory

 ptr = ptr + 1, switch the pointer to the next index of array


 ptr = ptr + 1, it will add 4 rather then 1 in final value of
pointer

Note: Pointer’s arithmetic is different from the arithmetic performed on


other data types

PAGE
Points to ponder

You might also like