Table of contents
1.
Introduction
2.
Addition of Integer to a Pointer
3.
Subtraction of Integer from a Pointer
4.
Increment Operation on a Pointer
5.
Decrement Operation on a Pointer
6.
Pointer Arithmetic on Arrays
7.
Comparison of Pointers
7.1.
C++
7.2.
C++
7.3.
C++
8.
FAQs
8.1.
What is the pointer operand in C?
8.2.
What is a pointer in C with an example?
8.3.
What are arithmetic expressions in C?
9.
Conclusion
Last Updated: Sep 21, 2024

Pointer Arithmetics

Author ANKIT KUMAR
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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 of Integer to a Pointer

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;
}
You can also try this code with Online C Compiler
Run Code

Output:

Address pointed by ptr before addition: 3079395180 
Address pointed by ptr after addition: 3079395192 
You can also try this code with Online C Compiler
Run Code

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.

Subtraction of Integer from a Pointer

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;
}
You can also try this code with Online C Compiler
Run Code

Output:

Address pointed by ptr before subtraction: 1750884940 
Address pointed by ptr after subtraction: 1750884928 
You can also try this code with Online C Compiler
Run Code

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;
}
You can also try this code with Online C Compiler
Run Code

Output:

Address pointed by ptr before increment: 2913933548 
Address pointed by ptr after increment: 2913933552 
You can also try this code with Online C Compiler
Run Code

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;
}
You can also try this code with Online C Compiler
Run Code

Output:

Address pointed by ptr before decrement: 3061363708 
Address pointed by ptr after decrement: 3061363704 
You can also try this code with Online C Compiler
Run Code

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++;
	}
}
You can also try this code with Online C Compiler
Run Code

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 
You can also try this code with Online C Compiler
Run Code

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.

Comparison of Pointers

In C++, pointers can be compared using relational operators such as `==`, `!=`, `<`, `>`, `<=`, and `>=`. The comparison of pointers is based on the memory addresses they point to, rather than the values stored at those addresses. Comparing pointers can be useful in various scenarios, like checking for equality, ordering, or determining the relative positions of memory addresses.

Explanation with Examples:

1. Equality Comparison (`==` and `!=`):

  • The `==` operator checks if two pointers point to the same memory address.
  • The `!=` operator checks if two pointers point to different memory addresses.


 Example: 

  • C++

C++

  int x = 5;
int y = 10;
int* ptr1 = &x;
int* ptr2 = &x;
int* ptr3 = &y;

if (ptr1 == ptr2) {
std::cout << "ptr1 and ptr2 point to the same address" << std::endl;
}
if (ptr1 != ptr3) {
std::cout << "ptr1 and ptr3 point to different addresses" << std::endl;
}
You can also try this code with Online C++ Compiler
Run Code


 Output:

ptr1 and ptr2 point to the same address
ptr1 and ptr3 point to different addresses    

 

2. Ordering Comparison (`<`, `>`, `<=`, and `>=`):

  • Pointers can be compared using the `<`, `>`, `<=`, and `>=` operators to determine the relative ordering of the memory addresses they point to.
  • The comparison is based on the numerical values of the memory addresses.


 Example:

  • C++

C++

 int arr[5] = {1, 2, 3, 4, 5};
int* ptr1 = &arr[0];
int* ptr2 = &arr[2];

if (ptr1 < ptr2) {
std::cout << "ptr1 points to a lower memory address than ptr2" << std::endl;
}
if (ptr2 > ptr1) {
std::cout << "ptr2 points to a higher memory address than ptr1" << std::endl;
}
You can also try this code with Online C++ Compiler
Run Code


Output:    

ptr1 points to a lower memory address than ptr2
ptr2 points to a higher memory address than ptr1    

 

3. Pointer Arithmetic Comparison:

  • Pointer arithmetic can be used to compare pointers that point to elements within the same array.
  • The comparison is based on the relative positions of the elements in the array.
     

Example:
    

  • C++

C++

  int arr[5] = {1, 2, 3, 4, 5};
int* ptr1 = &arr[0];
int* ptr2 = &arr[2];

if (ptr2 - ptr1 == 2) {
std::cout << "ptr2 is 2 elements ahead of ptr1" << std::endl;
}
if (ptr1 + 1 == &arr[1]) {
std::cout << "ptr1 + 1 points to the next element" << std::endl;
}
You can also try this code with Online C++ Compiler
Run Code


Output:

ptr2 is 2 elements ahead of ptr1
ptr1 + 1 points to the next element

It's important to note that comparing pointers that do not point to elements within the same array or object leads to undefined behavior. Pointer comparison is only meaningful when the pointers being compared are related and point to elements within the same memory block.

FAQs

What is the pointer operand in C?

The pointer operand in C is the * (asterisk) used to dereference pointers, accessing the value stored at the memory address they point to.

What is a pointer in C with an example?

A pointer in C is a variable that stores the memory address of another variable. Example: int* ptr = &var;.

What are arithmetic expressions in C?

Arithmetic expressions in C involve operators like +, -, *, /, % to compute numeric values, potentially involving variables and constants.

Conclusion

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

Live masterclass