Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Addition
3.
Subtraction
4.
Increment Operation on a Pointer
5.
Decrement Operation on a Pointer
6.
Pointer Arithmetic on Arrays
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

Pointer Arithmetics

Author ANKIT KUMAR
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

We are already familiar with what pointers are in C. They are also variables, but instead of storing a value like normal variables, they store the address of some other variable.

There are also various arithmetic operations that can be performed on the pointers. The operations are:

  • Addition of an integer to a pointer.
  • Subtraction of an integer from a pointer.
  • Incrementing pointer.
  • Decrementing pointer.

We shall now learn about each of the above operations in detail and try to understand them with the help of code implementation.

Also see: C Static Function, And Tribonacci Series

Addition

We can add an integer pointer. However, unlike the normal mathematical addition operation, addition in a pointer works in a different way. If we try to add a number to a pointer, the number is first multiplied by the size of the data type and then added to the pointer. The size of the data type depends on the system. For example, in a 32-bit system, the size of int is 2 bytes while it is 4 bytes on a 64-bit system.

Example:

Consider a situation where a pointer is pointing to a memory location 100. We assume it to be a 64-bit system. Now suppose we want to add some integer "x" to the pointer, then after addition, the pointer will point to the memory location: 100+(4*x).

For example, the pointer in the image (say ptr) is pointing to memory address 100. If we add 3 to it then the pointer will have a value 100+ 4*3= 112 inside it. That means it will now point to memory address 112, having a value 10.

Let us now see a simple program in C that will demonstrate how pointer addition works in real life.

// Program to demonstrate pointer addition in C
#include <stdio.h>

// Driver Code
int main()
{
	// Integer variable which is assigned a value 10
	int var = 10;

	// we create a Pointer to an integer
	int *ptr;

	// Pointer stores the address of the variable var which we created above
	ptr = &var;

	printf("Address pointed by ptr before addition: ");
	printf("%u \n", ptr);
    
    // adding 3 to the pointer ptr
    ptr = ptr+3;
    
	printf("Address pointed by ptr after addition: ");
	printf("%u \n", ptr);

	return 0;
}

Output:

Address pointed by ptr before addition: 3079395180 
Address pointed by ptr after addition: 3079395192 

Explanation:

In the above code, the ptr is a pointer pointing to the variable var. Initially, the value inside ptr, i.e., the address of var is 3079395180. When we add 3 to the pointer ptr, since the system is 64 bit, therefore 3 is multiplied by the size of int on a 64-bit system (4 bytes). The final value inside ptr is thus increased by 12, and it points to the memory address 3079395192.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Subtraction

Just like adding an integer to a pointer, we can subtract an integer from a pointer. If we try to subtract a number from a pointer, the number is first multiplied by the size of the data type and then subtracted from the pointer. The size of the data type depends on the system. For example, in a 32-bit system, the size of int is 2 bytes while it is 4 bytes on a 64-bit system.

Example: 

We shall take the help of the previous example to see subtraction in pointers.

Consider a situation where a pointer is pointing to a memory location 112. We assume it to be a 64-bit system. Now suppose we want to subtract some integer "x" from the pointer, then after subtraction, the pointer will point to the memory location: 100 - (4*x).

For example, the pointer in the image (say ptr) is pointing to memory address 112. If we subtract 3 from it, then the pointer will have a value 112 - (4*3)=100  inside it. That means it will now point to memory address 100 having value 7.

Code demonstration:

// Program to demonstrate pointer subtraction in C
#include <stdio.h>

// Driver Code
int main()
{
	// Integer variable which is assigned a value 10
	int var = 10;

	// we create a Pointer to an integer
	int *ptr;

	// Pointer stores the address of the variable var which we created above
	ptr = &var;

	printf("Address pointed by ptr before subtraction: ");
	printf("%u \n", ptr);
    
    // subtracting 3 from the pointer ptr
    ptr = ptr-3;
    
	printf("Address pointed by ptr after subtraction: ");
	printf("%u \n", ptr);

	return 0;
}

Output:

Address pointed by ptr before subtraction: 1750884940 
Address pointed by ptr after subtraction: 1750884928 

Explanation:

In the above code, the ptr is a pointer pointing to the variable var. Initially, the value inside ptr, i.e., the address of var is 1750884940. When we subtract 3 from the pointer ptr, since the system is 64 bit, therefore 3 is multiplied by the size of int on a 64-bit system (4 bytes). The final value inside ptr is thus decreased by 12, and it points to the memory address 1750884928.

Increment Operation on a Pointer

Incrementing a pointer is the same as adding 1 to the pointer. Depending on the system, whether it is a 64-bit system or a 32-bit system, the pointer gets incremented. For a 64 bit system, it gets incremented by 4 bytes, and for a 32-bit system, it gets incremented by 2 bytes.

Example:

Suppose a pointer is pointing to the memory address 100. Incrementing the pointer on a 64-bit system will make it point to the memory address 104, and on a 32-bit system, it will point to 102 after incrementing.

Pointer incrementation is often used while traversing an array.

Example:

// Program to demonstrate incrementing pointer in C
#include <stdio.h>


// Driver Code
int main()
{
	// Integer variable which is assigned a value 100
	int var = 100;


	// we create a Pointer to an integer
	int *ptr;


	// Pointer stores the address of the variable var which we created above
	ptr = &var;



	printf("Address pointed by ptr before increment: ");
	printf("%u \n", ptr);
    
    // incrementing the pointer
    ptr++;
    
	printf("Address pointed by ptr after increment: ");
	printf("%u \n", ptr);


	return 0;
}

Output:

Address pointed by ptr before increment: 2913933548 
Address pointed by ptr after increment: 2913933552 

Explanation:

Initially, the pointer was pointing to the memory address of var, which was 2913933548. After incrementing the pointer, its value increases by 4 because the size of the int data type on a 64-bit system is 4 bytes. The increased value of the pointer then becomes 2913933552.

Decrement Operation on a Pointer

Decrementing a pointer is the same as subtracting 1 from the pointer. Depending on the system, whether it is a 64-bit system or a 32-bit system, the pointer is decremented. For a 64 bit system, it gets decremented by 4 bytes, and for a 32-bit system, it gets decremented by 2 bytes.

Example:

Suppose a pointer is pointing to the memory address 100. Decrementing the pointer on a 64-bit system will make it point to the memory address 96, and on a 32-bit system, it will point to 98 after decrementing.

Example:

// Program to demonstrate decrementing pointer in C
#include <stdio.h>

// Driver Code
int main()
{
	// Integer variable which is assigned a value 100
	int var = 100;

	// we create a Pointer to an integer
	int *ptr;

	// Pointer stores the address of the variable var which we created above
	ptr = &var;

	printf("Address pointed by ptr before decrement: ");
	printf("%u \n", ptr);
    
    // decrementing the pointer
    ptr--;
    
	printf("Address pointed by ptr after decrement: ");
	printf("%u \n", ptr);

	return 0;
}

Output:

Address pointed by ptr before decrement: 3061363708 
Address pointed by ptr after decrement: 3061363704 

Explanation:

Initially, the pointer was pointing to the memory address of var, which was 3061363708. After decrementing the pointer, its value decreases by 4 because the size of the int data type on a 64-bit system is 4 bytes. The decreased value of the pointer then becomes 3061363704.

You can also read about the dynamic arrays in c and  Short int in C Programming

Pointer Arithmetic on Arrays

We know that pointers are variables that contain the address of some other variable. An array can be thought of as a contiguous memory block. Whenever we declare an array, the name of the array acts as a pointer variable that points to the first element in the array. When we want to traverse the array, it is equivalent to incrementing the pointer until the last element is reached.

Example:

// C program to illustrate pointer arithmetic on arrays

#include <stdio.h>

// Driver Code
int main()
{

	// An array
	int arr[] = { 10, 20, 30, 40, 50 };

	// Declare pointer variable
	int* ptr;

	// Point the pointer to first
	// element in array arr[]
	ptr = arr;

	// Traverse array using ptr
	for (int i = 0; i < 5; i++) {

		// Print the address as well as the value of the element at which
		// ptr points
		printf("currently pointing at %u . The value of the corresponding element is %d \n",ptr, ptr[0]);
		
		ptr++;
	}
}

Output:

currently pointing at 138853456 . The value of the corresponding element is 10 
currently pointing at 138853460 . The value of the corresponding element is 20 
currently pointing at 138853464 . The value of the corresponding element is 30 
currently pointing at 138853468 . The value of the corresponding element is 40 
currently pointing at 138853472 . The value of the corresponding element is 50 

Explanation:

Initially, ptr points to the very first element in the array. It means that ptr holds the address of arr[0]. Every time we increment the pointer using ptr++, it follows the increment operation which we studied above. The address increases by 4 bytes because of the 64-bit system after every iteration.

You can implement it by yourself with the help of an online C compiler.

FAQs

1.What is pointer arithmetic?
Pointer arithmetic refers to the arithmetics operation that can be legally performed on the pointers.

2. What are the various arithmetic operations that can be performed on the pointers?
The arithmetic operations are addition, subtraction, increment, and decrement.

3. Why is adding two pointers irrelevant?
Pointers hold the address of other variables. Nothing useful is achieved by adding the addresses of two variables. Therefore adding two pointers is irrelevant.

4. How does the system configuration affect pointers arithmetic?
On a 64 bit system, the address increases or decreases by 4 bytes, whereas on a 32-bit system, the address increases or decreases by 2 bytes.

Key Takeaways

In this article, we have extensively discussed pointer arithmetics along with its code implementation in the C programming language.

  • Pointer arithmetic refers to the arithmetics operation that can be legally performed on the pointers.
  • The size of the data type depends on the system. For example, in a 32-bit system, the size of int is 2 bytes while it is 4 bytes on a 64-bit system.
  • The various arithmetic operations that can be performed on the pointers are addition, subtraction, increment, and decrement.

We hope that this blog has helped you enhance your knowledge regarding pointer arithmetics and if you would like to learn more, check out our articles here. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Function Pointer
Next article
Advantages of Pointers in C
Live masterclass