The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C Pointer Arithmetic

C programming allow programmers just like you to do arithmetic operations using pointers. So, programmers can perform any arithmetic operations using pointer variables. Performing arithmetic operations using pointer variables is said to be arithmetic pointer.

pointer arithmetic in c

C Program - Pointer Arithmetic

pointers-arithmetic.c
#include <stdio.h>
int main()
{
int a = 10, b = 6;
int *ptr, *ptr1;
ptr = &a;
ptr1 = &b;
printf("sum of two pointers : %d \n", *ptr+b);
printf("Subtraction of two pointers : %d \n", a-*ptr1);
return 0;
}
  • sum of two pointers : 16
  • subtraction of two pointers : 4

Note:

In the above program, two integer pointers ptr and ptr1 are declared and initialized by the address of a and b respectively. Then, *ptr + b and a - *ptr1 is used to perform Addition and Subtraction using pointers.

Incrementing Pointer variable

Suppose if your interviewer ask you to display the value stored in an array without using any index value. Then Pointer will help you to get there by using increment operator.

Program - Incrementing Pointer variable

increment-pointer.c
#include <stdio.h>
int main()
{
int arr[3] = {10, 11, 12};
int *ptr, i;
ptr = arr;
for(i = 0; i < 3; i++)
{
printf("%d\t", *ptr);
ptr++;
}
return 0;
}
  • 10      11      12

Note:

Here pointer variable ptr is initialized with the address of first element in an array arr. We know that array stores the elements consecutively in it. Then its very simple for a pointer to point to the next memory location using increment operator.

Decrementing Pointer Variable

Suppose if your interviewer ask you to display the value stored in an array in reverse order by providing you with the address of last value in an array and restricting not to using index value iteratively. Then Pointer will help you to get there by using decrement operator.

Program - Decrementing Pointer variable

decrement-pointer.c
#include <stdio.h>
int main()
{
int a[3] = {10, 11, 12};
int *ptr, i;
ptr = &a[2];
for(i = 3; i > 0; i--)
{
printf("%d\t", *ptr);
ptr--;
}
return 0;
}
  • 12      11      10

Note:

Here pointer variable ptr is initialized with the address of last element in an array arr. We know that array stores the elements consecutively in it. Then its very simple for a pointer to point to the previous memory location using increment operator.

Comparing Pointer Variable

Pointer variable can be compared either with other pointer variable or with normal variable. Relational operators will get you there for comparison operation. Let us using greater than operator to compare two pointer variables.

Program - Comparing Pointer variable

comapring-pointer.c
#include <stdio.h>
int main()
{
int a = 10, b = 6;
int *ptr, *ptr1;
ptr = &a;
ptr1 = &b;
if(*ptr > *ptr1)
printf("value stored in a is greater than b ");
else
printf("value stored in b is greater than a ");
return 0;
}
  • value stored in a is greater than b

Note:

Here *ptr and *ptr1 will fetch the value from the address stored in it and greater than ( > ) operator helps us to find greatest among two pointer variables.

Did You Know?

Pointer variable can be used for all sort of operations like addition, subtraction, division, passing value and even more as we do with normal variable. But pointer variable can play with address of memory which cannot be done using normal variable is the only difference.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report