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

PPTX
Pointer arithmetic in c
PPTX
pointers.pptx
PPTX
L4_Pointer Arithmetic in C++.pptxhwhwjwjw
PPTX
Pointers and single &multi dimentionalarrays.pptx
PPT
13092119343434343432232323121211213435554
PPTX
FYBSC(CS)_UNIT-1_Pointers in C.pptx
PPTX
Array of pointer
PPTX
Pointer arithmetic in c
pointers.pptx
L4_Pointer Arithmetic in C++.pptxhwhwjwjw
Pointers and single &multi dimentionalarrays.pptx
13092119343434343432232323121211213435554
FYBSC(CS)_UNIT-1_Pointers in C.pptx
Array of pointer

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

PPTX
Ponters
PPTX
Pointer.pptx
PDF
Pointers
PPTX
Pf cs102 programming-9 [pointers]
PPTX
Pointer in C
PPT
Unit 6 pointers
PPTX
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
PPT
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
PPT
Pointers operation day2
PPTX
INTRODUCTION TO POINTER IN c++ AND POLYMORPHISM
PPTX
Pointers in c v5 12102017 1
PPTX
Pointers in c language
PPTX
Pointers
PDF
PSPC--UNIT-5.pdf
PPTX
CSE240 Pointers
PDF
C Pointers
PPT
chapter03.ppt easy words to understand and
PPTX
unit-7 Pointerdesfsdfsdgsdgaa notes.pptx
PPTX
Unit-I Pointer Data structure.pptx
PPTX
UNIT 4 POINTERS.pptx pointers pptx for basic c language
Ponters
Pointer.pptx
Pointers
Pf cs102 programming-9 [pointers]
Pointer in C
Unit 6 pointers
Array in C newrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Basics of pointer, pointer expressions, pointer to pointer and pointer in fun...
Pointers operation day2
INTRODUCTION TO POINTER IN c++ AND POLYMORPHISM
Pointers in c v5 12102017 1
Pointers in c language
Pointers
PSPC--UNIT-5.pdf
CSE240 Pointers
C Pointers
chapter03.ppt easy words to understand and
unit-7 Pointerdesfsdfsdgsdgaa notes.pptx
Unit-I Pointer Data structure.pptx
UNIT 4 POINTERS.pptx pointers pptx for basic c language
Ad

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Approach and Philosophy of On baking technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
MIND Revenue Release Quarter 2 2025 Press Release
Reach Out and Touch Someone: Haptics and Empathic Computing
20250228 LYD VKU AI Blended-Learning.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
cuic standard and advanced reporting.pdf
Electronic commerce courselecture one. Pdf
Network Security Unit 5.pdf for BCA BBA.
Review of recent advances in non-invasive hemoglobin estimation
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25 Week I
Machine learning based COVID-19 study performance prediction
Digital-Transformation-Roadmap-for-Companies.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The AUB Centre for AI in Media Proposal.docx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Understanding_Digital_Forensics_Presentation.pptx
Approach and Philosophy of On baking technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Ad

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; }