Updated April 20, 2023
Introduction to Pointer Arithmetic in C
The following article provides an outline for Pointer Arithmetic in C. As we are well aware that Pointers are one of the most interesting topics in C. Pointers are basically the variables which hold the address that points to a specific memory location accessed using ‘&’ operator. Pointers in C are used using the asterisk (*) operator before the name of the pointer. These addresses held by pointer variables are the integer values so the basic arithmetic operations can be performed on these pointers which will again result in an integer value (an address of a memory location).
Both the unary and binary operations can be performed on pointers like:
- Increment
- Decrement
- Addition (addition of any integer value to a pointer)
- Subtraction (either any integer value or the subtraction of 2 pointers)
- Comparison
All the above mentioned arithmetic operations can be performed on pointers as they are integers and nothing else. But some operations seem to be useless while performing as there is no idea of what they would result.
Pointer Arithmetic Operations along with Examples in C
Given below are the pointer arithmetic operations and their implementation in C code:
1. Increment
By incrementing the value to a pointer to 1, it will start pointing to the next address/ memory location. Incrementing the value of pointer is very useful while traversing the array in C. In order to access the next element of the array, we can simply use ptr++. Value is incremented according to the datatype of the value to which the pointer is pointing. For example, if the pointer is pointing to any integer value (having 64 bit integer), incrementing its value will increase its value by 4 whereas in case of ‘char’, value will increase by 1.
Code:
#include<stdio.h>
int main(){
int num =50;
char a = 'x';
// pointer 'ptr' to point the above 'num' and 'ptr1' to point 'a'
int *ptr;
char *ptr1;
// pointer 'ptr' holding the address of 'num' location and 'ptr1' to hold the address of character 'a'
ptr = #
ptr1 = &a;
printf("\n The address which the pointer holds is %u",ptr);
printf("\n The address which the pointer holds is %u",ptr1);
// incrementing the value of pointer by 1
ptr++;
ptr1++;
// Pointer address will now gets incremented by 4 bytes as it holds the address of integer value
printf("\n Now the address which the pointer holds is %u",ptr);
// Pointer address will now gets incremented by 1 byte as it holds the address of character value
printf("\n Now the address which the pointer holds is %u",ptr1);
return 0;
}
Output:
2. Decrement
Decrement operation works similar to the increment operation in case of pointers. Decrementing a pointer value using ‘ptr–’ will decrease its value by 1 resulting in the previous address of memory location. It will decrease the value of pointer by the number of bytes of the datatype it is pointing to.
Code:
#include<stdio.h>
int main(){
float num = 50.3;
char a = 'x';
// pointer 'ptr' to point the above 'num' and 'ptr1' to point 'a'
float *ptr;
char *ptr1;
// pointer 'ptr' holding the address of 'num' location and 'ptr1' to hold the address of character 'a'
ptr = #
ptr1 = &a;
printf("\n The address which the pointer holds is %u",ptr);
printf("\n The address which the pointer holds is %u",ptr1);
// decrementing the value of pointer by 1
ptr--;
ptr1--;
// Pointer address will now gets decremented by 4 bytes as it holds the address of float value
printf("\n Now the address which the pointer holds is %u",ptr);
// Pointer address will now gets decremented by 1 byte as it holds the address of character value
printf("\n Now the address which the pointer holds is %u",ptr1);
return 0;
}
Output:
3. Addition
We cannot add the two pointers, as it would also result in the address of an unknown memory location. So there is no use of it. But we can add any integer value to the pointer in order to point to that memory location. Addition of integer value works according to the datatype of the value pointer pointing to using ptr+x. For example, if the pointer holds the address of any integer value (64 – bit integer system having 4 bytes integer), on adding +2 in it, it will increment the value by 8 bytes.
Code:
#include<stdio.h>
int main(){
double num = 50.3;
char a = 'u';
// pointer 'ptr' to point the above 'num' and 'ptr1' to point 'a'
double *ptr;
char *ptr1;
// pointer 'ptr' holding the address of 'num' location and 'ptr1' to hold the address of character 'a'
ptr = #
ptr1 = &a;
printf("\n The address which the pointer holds is %u",ptr);
printf("\n The address which the pointer holds is %u",ptr1);
// adding the integer value 4 to the pointer value
ptr = ptr + 4;
ptr1 = ptr1 + 4;
// Pointer address will now gets incremented by 4*8 bytes as it holds the address of double value
printf("\n Now the address which the pointer holds is %u",ptr);
// Pointer address will now gets incremented by 4*1 bytes as it holds the address of character value
printf("\n Now the address which the pointer holds is %u",ptr1);
return 0;
}
Output:
4. Subtraction
Subtraction in case of pointers is possible with two addresses (i.e. with 2 pointer values) as well as subtraction of an integer value from the pointer. Subtraction of an integer value from the pointer works similar to the addition of integer value as discussed above, i.e. any integer value can be subtracted from the pointer using ptr-x. And it will result the difference of pointer by x * bytes of value datatype hold by pointer.
In subtraction of 2 pointers, both the pointers need to be of the same data type and it results in an integer value which is useful in case of arrays when we want to find the number of elements in between them using the 2 addresses.
Simple syntax to find the number of elements in between the 2 addresses of pointers is: (ptr2 – ptr1)/size of datatype that pointer holds.
Code:
#include<stdio.h>
int main(){
double num = 50.3;
char a = 'u';
// pointer 'ptr' to point the above 'num' and 'ptr1' to point 'a'
double *ptr;
char *ptr1;
// pointer 'ptr' holding the address of 'num' location and 'ptr1' to hold the address of character 'a'
ptr = #
ptr1 = &a;
printf("\n The address which the pointer holds is %u",ptr);
printf("\n The address which the pointer holds is %u",ptr1);
// subtracting the integer value 4 to the pointer value
ptr = ptr - 4;
ptr1 = ptr1 - 4;
// Pointer address will now gets decreased by 4*8 bytes as it holds the address of double value
printf("\n Now the address which the pointer holds is %u",ptr);
// Pointer address will now gets decreased by 4*1 bytes as it holds the address of character value
printf("\n Now the address which the pointer holds is %u",ptr1);
return 0;
}
Output:
5. Comparison
C provides a wonderful feature of comparing the 2 pointers with only the condition being that both the pointers are of the same type and pointing to the same array. All the comparison operations like (>, <, <=, >=, ==, !=) can be performed on them. Infact, C does not throw an error on the console in comparison of 2 pointers pointing to different datatype.
Code:
#include <stdio.h>
int main()
{
int arr1[6] = {100, 200, 300, 400, 500, 600};
// pointer 'ptr1' pointing to the address of 1st array element
int *ptr1 = &arr1[0];
printf("\n Array elements are given below:");
while(ptr1 < &arr1[6])
{
printf("\n array value is %d ", *ptr1);
//Incrementing the pointer to move to the address of next element
ptr1++;
}
return 0;
}
Output:
Conclusion
Above description clearly explains what are pointers and the various arithmetic operations that can be performed on them in C. Pointers are very useful when performing operations in arrays or in other data structures like linked list, stack, etc. One needs to understand them thoroughly before implementing them in the code as sometimes they return very absurd results. Like addition of 2 pointers is possible but there is no use of it as it would result in the address of some memory location which we don’t know.
Recommended Articles
This is a guide to Pointer Arithmetic in C. Here we discuss the introduction to Pointer Arithmetic in C with 5 arithmetic operations along with their examples. You may also have a look at the following articles to learn more –