Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
Applications of Pointers in C
Next article icon

Pointer Arithmetics in C with Examples

Last Updated : 24 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn't store any value. 

Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer arithmetic operations are slightly different from the ones that we generally use for mathematical calculations. These operations are:

  1. Increment/Decrement of a Pointer
  2. Addition of integer to a pointer
  3. Subtraction of integer to a pointer
  4. Subtracting two pointers of the same type
  5. Comparison of pointers

1. Increment/Decrement of a Pointer

Increment: It is a condition that also comes under addition. When a pointer is incremented, it actually increments by the number equal to the size of the data type for which it is a pointer. 

For Example:
If an integer pointer that stores address 1000 is incremented, then it will increment by 4(size of an int), and the new address will point to 1004. While if a float type pointer is incremented then it will increment by 4(size of a float) and the new address will be 1004.


Decrement: It is a condition that also comes under subtraction. When a pointer is decremented, it actually decrements by the number equal to the size of the data type for which it is a pointer. 

For Example:
If an integer pointer that stores address 1000 is decremented, then it will decrement by 4(size of an int), and the new address will point to 996. While if a float type pointer is decremented then it will decrement by 4(size of a float) and the new address will be 996.

pointer increment and decrement
 

Note: It is assumed here that the architecture is 64-bit and all the data types are sized accordingly. For example, integer is of 4 bytes.

Example of Pointer Increment and Decrement

Below is the program to illustrate pointer increment/decrement:

C
#include <stdio.h> // pointer increment and decrement //pointers are incremented and decremented by the size of the data type they point to  int main() {     int a = 22;     int *p = &a;     printf("p = %u\n", p); // p = 6422288     p++;     printf("p++ = %u\n", p); //p++ = 6422292    +4   // 4 bytes     p--;     printf("p-- = %u\n", p); //p-- = 6422288     -4   // restored to original value      float b = 22.22;     float *q = &b;     printf("q = %u\n", q);  //q = 6422284     q++;     printf("q++ = %u\n", q); //q++ = 6422288      +4   // 4 bytes     q--;     printf("q-- = %u\n", q); //q-- = 6422284       -4  // restored to original value      char c = 'a';     char *r = &c;     printf("r = %u\n", r);   //r = 6422283     r++;     printf("r++ = %u\n", r);   //r++ = 6422284     +1   // 1 byte     r--;     printf("r-- = %u\n", r);   //r-- = 6422283     -1  // restored to original value      return 0; } 

Output
p = 1441900792 p++ = 1441900796 p-- = 1441900792 q = 1441900796 q++ = 1441900800 q-- = 1441900796 r = 1441900791 r++ = 1441900792 r-- = 1441900791

Note: Pointers can be outputted using %p, since, most of the computers store the address value in hexadecimal form using %p gives the value in that form. But for simplicity and understanding we can also use %u to get the value in Unsigned int form.

2. Addition of Integer to Pointer

When a pointer is added with an integer value, the value is first multiplied by the size of the data type and then added to the pointer.

For Example:
Consider the same example as above where the ptr is an integer pointer that stores 1000 as an address. If we add integer 5 to it using the expression, ptr = ptr + 5, then, the final address stored in the ptr will be ptr = 1000 + sizeof(int) * 5 = 1020.

pointer addition
 

Example of Addition of Integer to Pointer

C
// C program to illustrate pointer Addition #include <stdio.h>  // Driver Code int main() {     // Integer variable     int N = 4;      // Pointer to an integer     int *ptr1, *ptr2;      // Pointer stores the address of N     ptr1 = &N;     ptr2 = &N;      printf("Pointer ptr2 before Addition: ");     printf("%p \n", ptr2);      // Addition of 3 to ptr2     ptr2 = ptr2 + 3;     printf("Pointer ptr2 after Addition: ");     printf("%p \n", ptr2);      return 0; } 

Output
Pointer ptr2 before Addition: 0x7ffca373da9c  Pointer ptr2 after Addition: 0x7ffca373daa8 

3. Subtraction  of Integer to Pointer

When a pointer is subtracted with an integer value, the value is first multiplied by the size of the data type and then subtracted from the pointer similar to addition.

For Example:
Consider the same example as above where the ptr is an integer pointer that stores 1000 as an address. If we subtract integer 5 from it using the expression, ptr = ptr - 5, then, the final address stored in the ptr will be ptr = 1000 - sizeof(int) * 5 = 980.

pointer substraction

Example of Subtraction of Integer from Pointer

Below is the program to illustrate pointer Subtraction:

C
// C program to illustrate pointer Subtraction #include <stdio.h>  // Driver Code int main() {     // Integer variable     int N = 4;      // Pointer to an integer     int *ptr1, *ptr2;      // Pointer stores the address of N     ptr1 = &N;     ptr2 = &N;      printf("Pointer ptr2 before Subtraction: ");     printf("%p \n", ptr2);      // Subtraction of 3 to ptr2     ptr2 = ptr2 - 3;     printf("Pointer ptr2 after Subtraction: ");     printf("%p \n", ptr2);      return 0; } 

Output
Pointer ptr2 before Subtraction: 0x7ffd718ffebc  Pointer ptr2 after Subtraction: 0x7ffd718ffeb0 

4. Subtraction of Two Pointers

The subtraction of two pointers is possible only when they have the same data type. The result is generated by calculating the difference between the addresses of the two pointers and calculating how many bytes of data it is according to the pointer data type. The subtraction of two pointers gives the increments between the two pointers. 

For Example: 
Two integer pointers say ptr1(address:1000) and ptr2(address:1004) are subtracted. The difference between addresses is 4 bytes. Since the size of int is 4 bytes, therefore the increment between ptr1 and ptr2 is given by (4/4) = 1.

Example of Subtraction of Two Pointer

Below is the implementation to illustrate the Subtraction of Two Pointers:

C
// C program to illustrate Subtraction // of two pointers #include <stdio.h>  // Driver Code int main() {     int x = 6; // Integer variable declaration     int N = 4;      // Pointer declaration     int *ptr1, *ptr2;      ptr1 = &N; // stores address of N     ptr2 = &x; // stores address of x      printf(" ptr1 = %u, ptr2 = %u\n", ptr1, ptr2);     // %p gives an hexa-decimal value,     // We convert it into an unsigned int value by using %u      // Subtraction of ptr2 and ptr1     x = ptr1 - ptr2;      // Print x to get the Increment     // between ptr1 and ptr2     printf("Subtraction of ptr1 "            "& ptr2 is %d\n",            x);      return 0; } 

Output
 ptr1 = 2715594428, ptr2 = 2715594424 Subtraction of ptr1 & ptr2 is 1

5. Comparison of Pointers

We can compare the two pointers by using the comparison operators in C. We can implement this by using all operators in C >, >=, <, <=, ==, !=.  It returns true for the valid condition and returns false for the unsatisfied condition. 

  1. Step 1: Initialize the integer values and point these integer values to the pointer.
  2. Step 2: Now, check the condition by using comparison or relational operators on pointer variables.
  3. Step 3: Display the output.

Example of Pointer Comparision

C
// C Program to illustrare pointer comparision #include <stdio.h>  int main() {     // declaring array     int arr[5];      // declaring pointer to array name     int* ptr1 = &arr;     // declaring pointer to first element     int* ptr2 = &arr[0];      if (ptr1 == ptr2) {         printf("Pointer to Array Name and First Element "                "are Equal.");     }     else {         printf("Pointer to Array Name and First Element "                "are not Equal.");     }      return 0; } 

Output
Pointer to Array Name and First Element are Equal.

Comparison to NULL

A pointer can be compared or assigned a NULL value irrespective of what is the pointer type. Such pointers are called NULL pointers and are used in various pointer-related error-handling methods.

C
// C Program to demonstrate the pointer comparison with NULL // value #include <stdio.h>  int main() {      int* ptr = NULL;      if (ptr == NULL) {         printf("The pointer is NULL");     }     else {         printf("The pointer is not NULL");     }     return 0; } 

Output
The pointer is NULL

Comparison operators on Pointers using an array

In the below approach, it results in the count of odd numbers and even numbers in an array. We are going to implement this by using a pointer.

  1. Step 1: First, declare the length of an array and array elements.
  2. Step 2: Declare the pointer variable and point it to the first element of an array.
  3. Step 3: Initialize the count_even and count_odd. Iterate the for loop and check the conditions for the number of odd elements and even elements in an array
  4. Step 4: Increment the pointer location ptr++ to the next element in an array for further iteration.
  5. Step 5: Print the result.

Example of Pointer Comparison in Array

C
// Pointer Comparision in Array #include <stdio.h>  int main() {     int n = 10; // length of an array      int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };     int* ptr; // Declaration of pointer variable      ptr = arr; // Pointer points the first (0th index)                // element in an array     int count_even = 0;     int count_odd = 0;      for (int i = 0; i < n; i++) {          if (*ptr % 2 == 0) {             count_even++;         }         if (*ptr % 2 != 0) {             count_odd++;         }         ptr++; // Pointing to the next element in an array     }     printf("No of even elements in an array is : %d",            count_even);     printf("\nNo of odd elements in an array is : %d",            count_odd); } 

Output
No of even elements in an array is : 5 No of odd elements in an array is : 5

Pointer Arithmetic on Arrays

Pointers contain addresses. Adding two addresses makes no sense because there is no idea what it would point to. Subtracting two addresses lets you compute the offset between the two addresses. An array name acts like a pointer constant. The value of this pointer constant is the address of the first element.

For Example: if an array is named arr then arr and &arr[0] can be used to reference the array as a pointer.

Below is the program to illustrate the Pointer Arithmetic on arrays:

Program 1: 

C
// C program to illustrate the array // traversal using pointers #include <stdio.h>  // Driver Code int main() {      int N = 5;      // An array     int arr[] = { 1, 2, 3, 4, 5 };      // 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 < N; i++) {          // Print element at which         // ptr points         printf("%d ", ptr[0]);         ptr++;     } } 

Output
1 2 3 4 5 

Program 2: 

C
// C program to illustrate the array // traversal using pointers in 2D array #include <stdio.h>  // Function to traverse 2D array // using pointers void traverseArr(int* arr, int N, int M) {      int i, j;      // Traverse rows of 2D matrix     for (i = 0; i < N; i++) {          // Traverse columns of 2D matrix         for (j = 0; j < M; j++) {              // Print the element             printf("%d ", *((arr + i * M) + j));         }         printf("\n");     } }  // Driver Code int main() {      int N = 3, M = 2;      // A 2D array     int arr[][2] = { { 1, 2 }, { 3, 4 }, { 5, 6 } };      // Function Call     traverseArr((int*)arr, N, M);     return 0; } 

Output
1 2  3 4  5 6 

Next Article
Applications of Pointers in C

V

vishalraina
Improve
Article Tags :
  • C Language
  • C-Pointers
  • Pointers
  • C-Advanced Pointer
  • C-Pointer Basics
Practice Tags :
  • Pointers

Similar Reads

    C Pointers
    A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it has the address where the value is stored in memory. This allows us to manipulate the data stored at a specific memory location without actually using its variable. It is the backbone of
    9 min read
    Pointer Arithmetics in C with Examples
    Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn't store any value. Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer
    10 min read
    Applications of Pointers in C
    Pointers in C are variables that are used to store the memory address of another variable. Pointers allow us to efficiently manage the memory and hence optimize our program. In this article, we will discuss some of the major applications of pointers in C. Prerequisite: Pointers in C. C Pointers Appl
    4 min read
    Passing Pointers to Functions in C
    Prerequisites: Pointers in CFunctions in C Passing the pointers to the function means the memory location of the variables is passed to the parameters in the function, and then the operations are performed. The function definition accepts these addresses using pointers, addresses are stored using po
    2 min read
    C - Pointer to Pointer (Double Pointer)
    In C, double pointers are those pointers which stores the address of another pointer. The first pointer is used to store the address of the variable, and the second pointer is used to store the address of the first pointer. That is why they are also known as a pointer to pointer.Let's take a look at
    5 min read
    Chain of Pointers in C with Examples
    Prerequisite: Pointers in C, Double Pointer (Pointer to Pointer) in CA pointer is used to point to a memory location of a variable. A pointer stores the address of a variable.Similarly, a chain of pointers is when there are multiple levels of pointers. Simplifying, a pointer points to address of a v
    5 min read
    Function Pointer in C
    In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
    6 min read
    How to Declare a Pointer to a Function?
    A pointer to a function is similar to a pointer to a variable. However, instead of pointing to a variable, it points to the address of a function. This allows the function to be called indirectly, which is useful in situations like callback functions or event-driven programming.In this article, we w
    2 min read
    Pointer to an Array | Array Pointer
    A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements.Example:C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr;
    5 min read
    Difference between constant pointer, pointers to constant, and constant pointers to constants
    In this article, we will discuss the differences between constant pointer, pointers to constant & constant pointers to constants. Pointers are the variables that hold the address of some other variables, constants, or functions. There are several ways to qualify pointers using const. Pointers to
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences