SlideShare a Scribd company logo
Pointer
Arithmetic
Paurush Sinha
Analogy: Incrementing and Decrementing Pointer
What if, we use increment or decrement operator in pointers, like we do with
integer data type.
Analogy with Integer
#include <stdio.h>
int main(){
int a = 1;
int b;
b = a++;
printf("a: %d b: %d", a, b);
return 0;
}
Output
a: 2 b: 1​
Implementation:
Incrementing and
Decrementing
Pointer
So if we follow similar syntax as followed
in last slide for integers, as shown in code
below.
#include <stdio.h>
int main(){
int *a = 1;
a*++;
printf("%d", *a);
return 0;
}
So, will it increment the value of integer stored in
the address of the pointer? Not really.
Theory: Incrementing and Decrementing Pointer
If you use increment or decrement operator on pointer, C will
increment or decrement pointer to respective memory address.
Hence, the code in previous slide will not increment the value,
instead will increment the pointer address.
Address 100 200 300
Value 1 2 3
Sounds Great?
Example of
Increment and
Decrement in
Pointers
Let's dive into the syntactically correct and
working example of the Pointer Arithmetic.
Code
#include <stdio.h>
int main(){
int a = 1;
int *b = &a;
printf("Value: %dn", *b);
*b++;
printf("Value: %d", *b);
return 0;
}
Output​
Value: 1​
Value: 1662454488
But, Why a random number?
After incrementing we can notice a random number because,
after incrementation, pointer has changed the address to
another memory location, so, the new target location may
already have a value stored by any other program running on
the computer, or may have some garbage value.
Address 100 200 300
Value 1454545 1 54654646
Using pointers to
iterate through arrays
Using pointers to iterate through arrays
Now, we know that increment and decrement operators,
increment to next pointer address and decrement to previous
pointer address respectively. Using this analogy we can
implement it in array. So, in array, we can use increment
operator on pointer to jump to next index of the array and
decrement operator to jump to previous index in array.
Aim
Let's say we have an array as
below.
int a[2] = {1,2,3};
We have to print all elements of
array using pointers.
Implementation
We can use the code below.
#include <stdio.h>
int main(){
int a[2] = {1,2,3};
int *b = &a[0];
for (int i=1;i<10;i++){
printf("%d. %dn", i, *b);
*b++;
}
return 0;
}
So we are running loop for 9 iterations, to show
you with some garbage values.
Output 1. 1
2. 2
3. 3
4. 927276744
5. 32765
6. 4198800
7. 0
8. -1679463158
9. 32626
Pointer and Function
How to pass
address to
function
You can pass address of a variable using address
operator. And get the value by creating pointer in
parameters in function.
Refer to the code below
#include <stdio.h>
void dothis(int *a){
printf("%d", *a);
}
int main(){
int b = 1;
dothis(&b);
return 0;
}

More Related Content

Similar to POINTERS IN C - BTECH BCA CS AND IT PRESENTATIONS

Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)tech4us
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
SwapnaliPawar27
 
C pointer
C pointerC pointer
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
WondimuBantihun1
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
janithlakshan1
 
Array Cont
Array ContArray Cont
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
aluavi
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
aarockiaabinsAPIICSE
 
C programming
C programmingC programming
C programming
Karthikeyan A K
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
Don Dooley
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
nmahi96
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans incnayakq
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
ajajkhan16
 
Programming egs
Programming egs Programming egs
Programming egs
Dr.Subha Krishna
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
sudhakargeruganti
 
unit 3 ppt.pptx
unit 3 ppt.pptxunit 3 ppt.pptx
unit 3 ppt.pptx
thenmozhip8
 
U3.pptx
U3.pptxU3.pptx

Similar to POINTERS IN C - BTECH BCA CS AND IT PRESENTATIONS (20)

Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)Pointers (Pp Tminimizer)
Pointers (Pp Tminimizer)
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
C pointer
C pointerC pointer
C pointer
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
pointers.pptx
pointers.pptxpointers.pptx
pointers.pptx
 
Chap 3 c++
Chap 3 c++Chap 3 c++
Chap 3 c++
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
Array Cont
Array ContArray Cont
Array Cont
 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
C programming
C programmingC programming
C programming
 
Pointers
PointersPointers
Pointers
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Qust & ans inc
Qust & ans incQust & ans inc
Qust & ans inc
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
Programming egs
Programming egs Programming egs
Programming egs
 
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdfEASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
EASY UNDERSTANDING OF POINTERS IN C LANGUAGE.pdf
 
unit 3 ppt.pptx
unit 3 ppt.pptxunit 3 ppt.pptx
unit 3 ppt.pptx
 
U3.pptx
U3.pptxU3.pptx
U3.pptx
 

Recently uploaded

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
Peter Spielvogel
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
Vlad Stirbu
 

Recently uploaded (20)

Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdfSAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
SAP Sapphire 2024 - ASUG301 building better apps with SAP Fiori.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Quantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIsQuantum Computing: Current Landscape and the Future Role of APIs
Quantum Computing: Current Landscape and the Future Role of APIs
 

POINTERS IN C - BTECH BCA CS AND IT PRESENTATIONS

  • 2. Analogy: Incrementing and Decrementing Pointer What if, we use increment or decrement operator in pointers, like we do with integer data type. Analogy with Integer #include <stdio.h> int main(){ int a = 1; int b; b = a++; printf("a: %d b: %d", a, b); return 0; } Output a: 2 b: 1​
  • 3. Implementation: Incrementing and Decrementing Pointer So if we follow similar syntax as followed in last slide for integers, as shown in code below. #include <stdio.h> int main(){ int *a = 1; a*++; printf("%d", *a); return 0; } So, will it increment the value of integer stored in the address of the pointer? Not really.
  • 4. Theory: Incrementing and Decrementing Pointer If you use increment or decrement operator on pointer, C will increment or decrement pointer to respective memory address. Hence, the code in previous slide will not increment the value, instead will increment the pointer address. Address 100 200 300 Value 1 2 3
  • 6. Example of Increment and Decrement in Pointers Let's dive into the syntactically correct and working example of the Pointer Arithmetic. Code #include <stdio.h> int main(){ int a = 1; int *b = &a; printf("Value: %dn", *b); *b++; printf("Value: %d", *b); return 0; } Output​ Value: 1​ Value: 1662454488
  • 7. But, Why a random number? After incrementing we can notice a random number because, after incrementation, pointer has changed the address to another memory location, so, the new target location may already have a value stored by any other program running on the computer, or may have some garbage value. Address 100 200 300 Value 1454545 1 54654646
  • 8. Using pointers to iterate through arrays
  • 9. Using pointers to iterate through arrays Now, we know that increment and decrement operators, increment to next pointer address and decrement to previous pointer address respectively. Using this analogy we can implement it in array. So, in array, we can use increment operator on pointer to jump to next index of the array and decrement operator to jump to previous index in array.
  • 10. Aim Let's say we have an array as below. int a[2] = {1,2,3}; We have to print all elements of array using pointers.
  • 11. Implementation We can use the code below. #include <stdio.h> int main(){ int a[2] = {1,2,3}; int *b = &a[0]; for (int i=1;i<10;i++){ printf("%d. %dn", i, *b); *b++; } return 0; } So we are running loop for 9 iterations, to show you with some garbage values.
  • 12. Output 1. 1 2. 2 3. 3 4. 927276744 5. 32765 6. 4198800 7. 0 8. -1679463158 9. 32626
  • 14. How to pass address to function You can pass address of a variable using address operator. And get the value by creating pointer in parameters in function. Refer to the code below #include <stdio.h> void dothis(int *a){ printf("%d", *a); } int main(){ int b = 1; dothis(&b); return 0; }