Back to C Programming
2025-12-305 min read

14.13 Pointer Increment and Decrement

Learn 14.13 Pointer Increment and Decrement step by step with clear examples and exercises.

Why This Matters

Pointer Increment and Decrement is a crucial concept in C programming that allows for direct manipulation of memory addresses, which is essential for efficient memory management, real-world applications, and performance optimization. Understanding pointer arithmetic will help you navigate complex data structures such as arrays, linked lists, and dynamic memory allocation more effectively.

Prerequisites

Before diving into Pointer Increment and Decrement, it is important to have a solid foundation in C programming basics, including variables, data types, operators, control structures, functions, and arrays. Additionally, familiarity with pointers and basic pointer arithmetic will be beneficial for understanding this topic more easily.

Important Concepts to Review:

  • Pointers
  • Arrays
  • Data Types
  • Operators
  • Control Structures
  • Functions

Core Concept

The ++ operator increments the value it is applied to by 1. In the context of pointers, this means advancing the memory address that the pointer points to by the size of the data type it points to. For example:

int arr[5] = {1, 2, 3, 4, 5};
int *p = arr; // p now points to arr[0]

// Incrementing p advances it to arr[1]
p++;

The -- operator decrements the value it is applied to by 1. This can be used to step backwards through an array or any other data structure.

Pointer Arithmetic Rules

  1. Pointers can be incremented and decremented using the ++ and -- operators, respectively.
  2. The result of adding or subtracting integers to pointers depends on the size of the data type they point to:
int *p = arr; // p points to arr[0]
p += 3; // p now points to arr[3]
  1. Pointers can be subtracted from integers to find the distance between two pointers (assuming they point to elements of the same data type):
int *p1 = arr;
int *p2 = p1 + 3; // p2 points to arr[3]
int dist = p2 - p1; // dist is 3
  1. Pointer arithmetic follows the following order of operations: *, &, [], and then + or -. This means that you should be careful when combining these operators to avoid unexpected results.

Worked Example

Let's consider a simple example of using pointer arithmetic to sum the elements in an array up to a certain point:

#include <stdio.h>

int sum_array(int *arr, int size) {
int sum = 0;
for (int *p = arr; p < arr + size; p++) {
sum += *p;
}
return sum;
}

int main() {
int arr[5] = {1, 2, 3, 4, 5};
printf("Sum of array elements: %d\n", sum_array(arr, sizeof(arr) / sizeof(arr[0])));
return 0;
}

In this example, the sum_array function takes an array and its size as arguments. It initializes a variable sum to 0 and uses a for loop to iterate through the array using a pointer p. The loop condition checks if p has reached the end of the array (i.e., arr + size). Inside the loop, *p dereferences the current memory address pointed to by p, adding its value to the running total sum.

Common Mistakes

  1. Forgetting to increment/decrement the pointer: If you forget to advance the pointer after dereferencing it, you may end up accessing invalid memory addresses.
int *p = arr; // p points to arr[0]
*p++; // Dereferences the next memory address (undefined behavior)
  1. Incorrect pointer arithmetic: Be careful when adding or subtracting integers to pointers, ensuring that you use the correct data type size and follow the proper order of operations.
  1. Accessing out-of-bounds array elements: Using incorrect pointer arithmetic can lead to accessing memory outside of the allocated array, resulting in undefined behavior and potential security vulnerabilities.
  1. ### Subheadings under Common Mistakes:
  • Forgetting to check for null pointers before performing pointer arithmetic operations.
  • Incorrectly assuming that pointer arithmetic works the same way with multi-dimensional arrays as it does with one-dimensional arrays.
  • Failing to account for pointer aliasing, which can lead to unintended consequences when multiple pointers are manipulated simultaneously.

Practice Questions

  1. Write a function that finds the sum of all even numbers in an array using pointer arithmetic.
  2. Implement a function that reverses an array using pointers and pointer arithmetic.
  3. Given two arrays, write a function that determines whether they contain the same elements (in any order) using pointers and pointer arithmetic.
  4. Write a function to find the maximum element in an array using pointer arithmetic.
  5. Implement a function that sorts an array of integers using bubble sort algorithm with pointer arithmetic.
  6. Write a function to search for a specific value in an array using pointer arithmetic and binary search.
  7. Implement a linked list data structure using pointers and pointer arithmetic, and write functions to insert, delete, and traverse the list.
  8. Write a function that compares two strings lexicographically using pointer arithmetic.
  9. Implement a function that counts the number of occurrences of a specific character in a string using pointer arithmetic.
  10. Write a function to find the longest common subsequence between two strings using pointer arithmetic and dynamic programming.

FAQ

  1. Why can't I increment/decrement a string pointer like an integer pointer?: Strings in C are null-terminated arrays of characters, so incrementing a string pointer by 1 advances it to the next character, not the next byte. To advance by a word (4 bytes on most systems), you would need to increment the pointer by 4.
  2. Can I use pointer arithmetic with multi-dimensional arrays?: Yes, but be careful when accessing elements as the memory layout of multi-dimensional arrays is not always contiguous in memory.
  3. What happens if I dereference a null pointer using pointer arithmetic?: Dereferencing a null pointer results in undefined behavior, which can lead to crashes or security vulnerabilities. Always ensure that your pointers are initialized and point to valid memory addresses before performing any operations on them.
  4. ### Subheadings under FAQ:
  • What is the difference between pointer arithmetic and array indexing?
  • How does pointer arithmetic affect structure members?
  • Can I use pointer arithmetic with pointers to functions?
  • Why can't I assign different data types to the same pointer without a typecast?
  • How does the size of a pointer vary between different platforms and architectures?