Understand pointer arithmetic in C in easy way – 3

Pointer arithmetic in C programming

Let’s suppose we created a pointer *ptr is pointing towards an int variable x which address is 2000. Which means ptr = 2000 because it is storing the address of int variable x.
Now if we shift pointer forward then pointer will start pointing 2004 because from 2000 to 2003, 4 bytes are used to store the value of int variable x.
Hence pointer will not point from 2001 to 2003 memory locations in this scenario, it will shift to 2004 memory location.

Same goes with char and others.
Now the question arises “Which operators are used to increment and decrement pointers?”
There are few arithmetic operators like ++, ——, – and + which can be use to shift pointers forward and backward.
We can also use comparison operators with pointers.

Increment a pointer

We can shift pointer forward using two operators those are ++, += and + operators.

In below example we can also use ptr+=1 or ptr = ptr + 1 instade of ptr++.

Both will give same output.

#include <stdio.h>
int main ()
{
int i,nums[]={1,2,3,4,5};
int *ptr = nums;
for(i = 0; i < 5; i++)
{
printf("n nums[%d] = %d -> value : %d",i,ptr,*ptr);
ptr++;
}
return 0;
}

Output : 

nums[0] = 6422292 -> value : 1
nums[1] = 6422296 -> value : 2
nums[2] = 6422300 -> value : 3
nums[3] = 6422304 -> value : 4
nums[4] = 6422308 -> value : 5

Decrement a pointer

We can shift pointer backward using two operators those are ——, -= and operators.

In below example we can also use ptr-=1 or ptr = ptr – 1 instade of ptr——.

Both will give same output.

#include <stdio.h>
int main ()
{
int i,nums[]={1,2,3,4,5};
int *ptr = &nums[4];
for(i = 4; i >= 0; i--)
{
printf("n nums[%d] = %d -> value : %d",i,ptr,*ptr);
ptr--;
}
return 0;
}




Output : 

nums[4] = 6422308 -> value : 5
nums[3] = 6422304 -> value : 4
nums[2] = 6422300 -> value : 3
nums[1] = 6422296 -> value : 2
nums[0] = 6422292 -> value : 1

Comparison operator with pointer

We can also use comparison operators to compare value pointer with other variable’s address or with value of another pointer.

#include <stdio.h>
int main ()
{
int i=0,nums[]={1,2,3,4,5};
int *ptr = nums;
while(ptr < &nums[5])
{
printf("n nums[%d] = %d -> value : %d",nums[i],ptr,*ptr);
ptr++;
i++;
}
return 0;
}




Output : 

nums[1] = 6422292 -> value : 1
nums[2] = 6422296 -> value : 2
nums[3] = 6422300 -> value : 3
nums[4] = 6422304 -> value : 4
nums[5] = 6422308 -> value : 5

CHECKOUT OTHER RELATED TOPICS

CHECKOUT OTHER QUIZZES

PRACTICALS IN C PROGRAM

Programs to understand the basic data types and I/O.
Programs on Operators and Expressions.
Programs on decision statements.
Programs on looping.
Programs on arrays.
Programs on functions.
Programs on structures and unions.
Programs on pointers.
Programs on string manipulations.
Practice Programs
1 – C program for leap year, easy and simple code
2 – Best C program to find sum of all digits
3 – Best C program to check Palindrome Number
4 – Best C program to check prime number
5 – Best C program to print Fibonacci series
6 – Best C program to reverse a number
7 – Best C program to reverse an array
8 – Best C program to find average of array
9 – Best C program to find frequency of array element
10 – Best C program to sort array in ascending order
11 – Best C program to find factorial using recursion
12 – Best C program to find sum of all numbers in array
13 – Best C program to find largest element in array
14 – Best C program to swap two numbers without third variable
15 – C Program to check if a number is even or odd
16 – C program to find Simple interest
17 – Best C program to add two matrices
18 – Best C program to count vowels and consonants in a string
19 – Best C program to find LCM
20 – Best C program to find HCF
21 – Best C program to swap two numbers using pointers
22 – Best C program to add two numbers using pointers
23 – Best C program to print Multiplication Table
24 – Best C program to check Armstrong number
25 – Best C program to print half pyramid of star(*)
26 – Best C program to print half pyramid of numbers
27 – Best C program to print reverse half pyramid of star
28 – Best C program to print reverse half pyramid of number
More coming soon…
We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment