SlideShare a Scribd company logo
Pointer Arithmetic
Prepared By: Mr. S. A. Patil
Assistant Professor,PVPIT Budhgaon
Pointer Arithmetic
 A pointer in c is an address, which is a numeric value.
 Therefore, we can perform arithmetic operations on a pointer just as you can on a
numeric value.
 A limited set of arithmetic operations can be performed on pointers. A pointer may
be:
1. Incremented ( ++ )
2. Decremented ( — )
3. An integer may be added to a pointer ( + or += )
4. An integer may be subtracted from a pointer ( – or -= )
 Pointers contain addresses. Adding two addresses makes no sense, because there
is no idea what it would point to.
 Subtracting two addresses lets you compute the offset between these two
addresses.
Incrementing a Pointer
 If we increment a pointer by 1, the pointer will start pointing to the immediate next
location.
 This is different from the general arithmetic because the value of the pointer will
get increased by the size of the data type to which the pointer is pointing.
 The Rule to increment the pointer is given below:
new_address= current_address + i * size_of(data type)
 Where i is the number by which the pointer get increased.
 We can traverse an array by using pointer.
Incrementing a Pointer
Example:
int a=10;
int *p;
p=&a;
p=p+1;
Here suppose address of a is 2010 which is stored in pointer variable p;
We perform increment operation on pointer variable p. then new value of p becomes.
p=p+1*2=2010+2=2012
Decrementing Pointer in C
 Like increment, we can decrement a pointer variable. If we decrement a pointer, it
will start pointing to the previous location. The formula of decrementing the
pointer is given below:
new_address= current_address - i * size_of(data type)
Example:
int a=10;
int *p;
p=&a;
p=p-1;
Here suppose address of a is 2010 which is stored in pointer variable p;
We perform decrement operation on pointer variable p. then new value of p becomes.
p=p-1*2=2010-2=2012
Pointer Addition
 We can add a value to the pointer variable. The formula of adding value to
pointer is given below:
new_address= current_address + (number * size_of(data type))
Example:
float a=10;
float *p;
p=&a;
p=p+3;
Here suppose address of a is 2410 which is stored in pointer variable p;
We add value 3 in pointer variable p. then new value of p becomes the address.
p=p+(3*4)=2410+12=2422
 We can not add two pointers in each other
Pointer Subtraction
 Like pointer addition, we can subtract a value from the pointer variable.
 Subtracting any number from a pointer will give an address.
 The formula of subtracting value from the pointer variable is given below:
new_address= current_address - (number * size_of(data type))
Example:
float a=10;
float *p;
p=&a;
p=p-2;
Here suppose address of a is 2410 which is stored in pointer variable p;
We subtract value 2 from pointer variable p. then new value of p becomes the address.
p=p-(2*2)=2410-4=2406
 We can subtract two pointers from each other to get offset.
Illegal arithmetic with pointers
 Address + Address = illegal
 Address * Address = illegal
 Address % Address = illegal
 Address / Address = illegal
 Address & Address = illegal
 Address ^ Address = illegal
 Address | Address = illegal
 ~Address = illegal

More Related Content

What's hot

Python tuple
Python   tuplePython   tuple
Python tuple
Mohammed Sikander
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
Tanmay Modi
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
Functions in c
Functions in cFunctions in c
Functions in c
sunila tharagaturi
 
File in C language
File in C languageFile in C language
File in C language
Manash Kumar Mondal
 
Pointer in c
Pointer in cPointer in c
Pointer in c
lavanya marichamy
 
Type conversion
Type  conversionType  conversion
Type conversion
PreethaPreetha5
 
pointers
pointerspointers
pointers
teach4uin
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
Mauryasuraj98
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Rajat Busheheri
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
programming9
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++Danial Mirza
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Monishkanungo
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 

What's hot (20)

Python tuple
Python   tuplePython   tuple
Python tuple
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
String functions in C
String functions in CString functions in C
String functions in C
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Functions in c
Functions in cFunctions in c
Functions in c
 
File in C language
File in C languageFile in C language
File in C language
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 
Type conversion
Type  conversionType  conversion
Type conversion
 
pointers
pointerspointers
pointers
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Strings in C
Strings in CStrings in C
Strings in C
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
Data Type Conversion in C++
Data Type Conversion in C++Data Type Conversion in C++
Data Type Conversion in C++
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Functions in C
Functions in CFunctions in C
Functions in C
 

Similar to Pointer arithmetic in c

Pointers operation day2
Pointers operation day2Pointers operation day2
Pointers operation day2
Bhuvana Gowtham
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
ArshiniGubbala3
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
janithlakshan1
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
Ramakrishna Reddy Bijjam
 
Pointer in C
Pointer in CPointer in C
Pointer in C
bipchulabmki
 
pointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptxpointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptx
AmitRout25
 
C pointer
C pointerC pointer
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
ajajkhan16
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
tanmaymodi4
 
POINTERS IN C - BTECH BCA CS AND IT PRESENTATIONS
POINTERS IN C - BTECH BCA CS AND IT PRESENTATIONSPOINTERS IN C - BTECH BCA CS AND IT PRESENTATIONS
POINTERS IN C - BTECH BCA CS AND IT PRESENTATIONS
paurushsinhad
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Jayanshu Gundaniya
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
sajinis3
 
Pointers
PointersPointers
Pointers
Vardhil Patel
 
chapter-11-pointers.pdf
chapter-11-pointers.pdfchapter-11-pointers.pdf
chapter-11-pointers.pdf
study material
 
Pointers
PointersPointers
Pointers
Prasadu Peddi
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
nmahi96
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
Vikram Nandini
 
Programming in Computer Science- Pointers in C++.ppt
Programming in Computer Science- Pointers in C++.pptProgramming in Computer Science- Pointers in C++.ppt
Programming in Computer Science- Pointers in C++.ppt
richardbaahnkansah
 
Pointers in c
Pointers in cPointers in c
Pointers in c
CHANDAN KUMAR
 

Similar to Pointer arithmetic in c (20)

Pointers operation day2
Pointers operation day2Pointers operation day2
Pointers operation day2
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
pointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptxpointers of the programming in c most efficient.pptx
pointers of the programming in c most efficient.pptx
 
C pointer
C pointerC pointer
C pointer
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
Pointers in c v5 12102017 1
Pointers in c v5 12102017 1Pointers in c v5 12102017 1
Pointers in c v5 12102017 1
 
POINTERS IN C - BTECH BCA CS AND IT PRESENTATIONS
POINTERS IN C - BTECH BCA CS AND IT PRESENTATIONSPOINTERS IN C - BTECH BCA CS AND IT PRESENTATIONS
POINTERS IN C - BTECH BCA CS AND IT PRESENTATIONS
 
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Pointers
PointersPointers
Pointers
 
chapter-11-pointers.pdf
chapter-11-pointers.pdfchapter-11-pointers.pdf
chapter-11-pointers.pdf
 
Pointers
PointersPointers
Pointers
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
Programming in Computer Science- Pointers in C++.ppt
Programming in Computer Science- Pointers in C++.pptProgramming in Computer Science- Pointers in C++.ppt
Programming in Computer Science- Pointers in C++.ppt
 
Lect 9(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer AbbasLect 9(pointers) Zaheer Abbas
Lect 9(pointers) Zaheer Abbas
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 

More from sangrampatil81

Deadlock
DeadlockDeadlock
Deadlock
sangrampatil81
 
Memory Management
Memory ManagementMemory Management
Memory Management
sangrampatil81
 
virtual memory
virtual memoryvirtual memory
virtual memory
sangrampatil81
 
IO hardware
IO hardwareIO hardware
IO hardware
sangrampatil81
 
File system structure
File system structureFile system structure
File system structure
sangrampatil81
 
File management
File managementFile management
File management
sangrampatil81
 
Disk structure
Disk structureDisk structure
Disk structure
sangrampatil81
 
Directory structure
Directory structureDirectory structure
Directory structure
sangrampatil81
 
Directory implementation and allocation methods
Directory implementation and allocation methodsDirectory implementation and allocation methods
Directory implementation and allocation methods
sangrampatil81
 
Page replacement algorithms
Page replacement algorithmsPage replacement algorithms
Page replacement algorithms
sangrampatil81
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
sangrampatil81
 
Semaphore
SemaphoreSemaphore
Semaphore
sangrampatil81
 
Monitors
MonitorsMonitors
Monitors
sangrampatil81
 
Classical problems of process synchronization
Classical problems of process synchronizationClassical problems of process synchronization
Classical problems of process synchronization
sangrampatil81
 
System programs
System programsSystem programs
System programs
sangrampatil81
 
System programs
System programsSystem programs
System programs
sangrampatil81
 
Services and system calls
Services and system callsServices and system calls
Services and system calls
sangrampatil81
 
Operating system structure
Operating system structureOperating system structure
Operating system structure
sangrampatil81
 
Operating system deign and implementation
Operating system deign and implementationOperating system deign and implementation
Operating system deign and implementation
sangrampatil81
 
Pointer to array and structure
Pointer to array and structurePointer to array and structure
Pointer to array and structure
sangrampatil81
 

More from sangrampatil81 (20)

Deadlock
DeadlockDeadlock
Deadlock
 
Memory Management
Memory ManagementMemory Management
Memory Management
 
virtual memory
virtual memoryvirtual memory
virtual memory
 
IO hardware
IO hardwareIO hardware
IO hardware
 
File system structure
File system structureFile system structure
File system structure
 
File management
File managementFile management
File management
 
Disk structure
Disk structureDisk structure
Disk structure
 
Directory structure
Directory structureDirectory structure
Directory structure
 
Directory implementation and allocation methods
Directory implementation and allocation methodsDirectory implementation and allocation methods
Directory implementation and allocation methods
 
Page replacement algorithms
Page replacement algorithmsPage replacement algorithms
Page replacement algorithms
 
Methods for handling deadlock
Methods for handling deadlockMethods for handling deadlock
Methods for handling deadlock
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Monitors
MonitorsMonitors
Monitors
 
Classical problems of process synchronization
Classical problems of process synchronizationClassical problems of process synchronization
Classical problems of process synchronization
 
System programs
System programsSystem programs
System programs
 
System programs
System programsSystem programs
System programs
 
Services and system calls
Services and system callsServices and system calls
Services and system calls
 
Operating system structure
Operating system structureOperating system structure
Operating system structure
 
Operating system deign and implementation
Operating system deign and implementationOperating system deign and implementation
Operating system deign and implementation
 
Pointer to array and structure
Pointer to array and structurePointer to array and structure
Pointer to array and structure
 

Recently uploaded

Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 

Recently uploaded (20)

Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 

Pointer arithmetic in c

  • 1. Pointer Arithmetic Prepared By: Mr. S. A. Patil Assistant Professor,PVPIT Budhgaon
  • 2. Pointer Arithmetic  A pointer in c is an address, which is a numeric value.  Therefore, we can perform arithmetic operations on a pointer just as you can on a numeric value.  A limited set of arithmetic operations can be performed on pointers. A pointer may be: 1. Incremented ( ++ ) 2. Decremented ( — ) 3. An integer may be added to a pointer ( + or += ) 4. An integer may be subtracted from a pointer ( – or -= )  Pointers contain addresses. Adding two addresses makes no sense, because there is no idea what it would point to.  Subtracting two addresses lets you compute the offset between these two addresses.
  • 3. Incrementing a Pointer  If we increment a pointer by 1, the pointer will start pointing to the immediate next location.  This is different from the general arithmetic because the value of the pointer will get increased by the size of the data type to which the pointer is pointing.  The Rule to increment the pointer is given below: new_address= current_address + i * size_of(data type)  Where i is the number by which the pointer get increased.  We can traverse an array by using pointer.
  • 4. Incrementing a Pointer Example: int a=10; int *p; p=&a; p=p+1; Here suppose address of a is 2010 which is stored in pointer variable p; We perform increment operation on pointer variable p. then new value of p becomes. p=p+1*2=2010+2=2012
  • 5. Decrementing Pointer in C  Like increment, we can decrement a pointer variable. If we decrement a pointer, it will start pointing to the previous location. The formula of decrementing the pointer is given below: new_address= current_address - i * size_of(data type) Example: int a=10; int *p; p=&a; p=p-1; Here suppose address of a is 2010 which is stored in pointer variable p; We perform decrement operation on pointer variable p. then new value of p becomes. p=p-1*2=2010-2=2012
  • 6. Pointer Addition  We can add a value to the pointer variable. The formula of adding value to pointer is given below: new_address= current_address + (number * size_of(data type)) Example: float a=10; float *p; p=&a; p=p+3; Here suppose address of a is 2410 which is stored in pointer variable p; We add value 3 in pointer variable p. then new value of p becomes the address. p=p+(3*4)=2410+12=2422  We can not add two pointers in each other
  • 7. Pointer Subtraction  Like pointer addition, we can subtract a value from the pointer variable.  Subtracting any number from a pointer will give an address.  The formula of subtracting value from the pointer variable is given below: new_address= current_address - (number * size_of(data type)) Example: float a=10; float *p; p=&a; p=p-2; Here suppose address of a is 2410 which is stored in pointer variable p; We subtract value 2 from pointer variable p. then new value of p becomes the address. p=p-(2*2)=2410-4=2406  We can subtract two pointers from each other to get offset.
  • 8. Illegal arithmetic with pointers  Address + Address = illegal  Address * Address = illegal  Address % Address = illegal  Address / Address = illegal  Address & Address = illegal  Address ^ Address = illegal  Address | Address = illegal  ~Address = illegal