Back to C Programming
2026-07-126 min read

Incrementing a Pointer with (++)

Learn Incrementing a Pointer with (++) step by step with clear examples and exercises.

Why This Matters

Incrementing a pointer with ++ is a fundamental concept in C programming that can help you manipulate memory addresses effectively. Understanding this operation will equip you with the tools needed to navigate complex data structures, optimize your code, and debug common issues more efficiently. In real-world scenarios, such as when working on projects involving dynamic memory allocation or linked lists, being proficient in pointer arithmetics can be the difference between a successful project and a frustrating dead end.

Prerequisites

Before diving into incrementing pointers with ++, you should have a good understanding of the following topics:

  1. Variables and memory allocation in C
  2. Pointers and their declaration
  3. Array basics and pointer notation
  4. Basic input/output operations using functions like printf() and scanf()
  5. Understanding the concept of memory addresses and how pointers work

Core Concept

In C, a pointer is a variable that stores the memory address of another variable. When you use the increment operator (++) with a pointer, it increases the memory address that the pointer points to by the size of the data type it's pointing to. This allows you to traverse through arrays and manipulate memory more efficiently.

Pointer basics

Before we dive into pointer arithmetics, let's briefly review how pointers work in C:

  1. A pointer is a variable that stores the memory address of another variable.
  2. To declare a pointer, use the * symbol followed by the data type you want to point to and the variable name. For example, int *ptr; declares a pointer that can store the address of an integer variable.
  3. To assign a value to a pointer, use the address-of operator (&) followed by the variable you want to point to. For example, ptr = &var; assigns the memory address of var to ptr.
  4. To access the value stored at the memory address pointed to by a pointer, use the dereference operator (*) before the pointer variable. For example, *ptr retrieves the value stored at the memory address pointed to by ptr.

Pointer arithmetics

Now that we've reviewed pointers let's explore how you can perform arithmetic operations on them:

  1. Incrementing a pointer with ++ increases the memory address it points to by the size of the data type it's pointing to. For example, if ptr is a pointer to an integer and *ptr currently points to the memory address 0x001020, after ptr++, it will point to 0x001024 (assuming an int takes up 4 bytes).
  1. Decrementing a pointer with -- decreases the memory address it points to by the size of the data type it's pointing to. For example, if ptr is a pointer to an integer and *ptr currently points to the memory address 0x001024, after ptr--, it will point to 0x001020.
  1. Adding an integer value to a pointer moves the pointer forward by that number of times the size of the data type it's pointing to. For example, if ptr is a pointer to an integer and *ptr currently points to the memory address 0x001020, after ptr += 2, it will point to 0x001028 (assuming an int takes up 4 bytes).

Pointer and array relationship

Arrays in C are simply contiguous blocks of memory allocated for a specific data type. When you declare an array, the compiler automatically calculates its starting address based on the variable name and the size of each element. Since pointers can store memory addresses, they provide a convenient way to traverse arrays and manipulate their elements.

In C, when you use pointer notation (e.g., arr[i]) with an array, it's equivalent to using the address-of operator (&) on the i-th element of the array followed by the dereference operator (*). For example:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // ptr now points to the memory address of arr[0]
printf("%d", *ptr); // prints 1 (the value stored at arr[0])

Using pointers and pointer arithmetics allows you to traverse arrays more efficiently, as demonstrated in our worked example below.

Worked Example

Let's walk through an example that demonstrates how to increment a pointer using the ++ operator:

#include <stdio.h>

int main() {
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // initialize ptr to point to the first element of the array

printf("Initial values:\n");
for (int i = 0; i < 5; i++) {
printf("arr[%d] = %d\n", i, arr[i]);
}

printf("\nTraversing with pointer arithmetics:\n");
for (int i = 0; i < 5; i++) {
printf("ptr = %p, *ptr = %d\n", ptr, *ptr);
ptr++; // increment the pointer to point to the next element in the array
}

return 0;
}

When you run this code, it will output:

Initial values:
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

Traversing with pointer arithmetics:
ptr = 0x7ffee6bff9c4, *ptr = 1
ptr = 0x7ffee6bff9c8, *ptr = 2
ptr = 0x7ffee6bff9cc, *ptr = 3
ptr = 0x7ffee6bff9d0, *ptr = 4
ptr = 0x7ffee6bff9d4, *ptr = 5

As you can see, the code initializes an array of integers and a pointer that points to the first element. It then prints both the current memory address pointed to by the pointer and the value stored at that address for each iteration of the loop. The pointer is incremented with ptr++, allowing it to point to the next element in the array for each iteration.

Common Mistakes

  1. Forgetting to dereference a pointer: When working with pointers, you must always use the dereference operator (*) to access the value stored at the memory address pointed to by the pointer. For example, instead of ptr = 5;, you should write *ptr = 5;.
  1. Incrementing a pointer before dereferencing it: When using pointer arithmetics, always remember to first dereference the pointer (*ptr) before incrementing or decrementing it (ptr++). Incorrect usage like ptr++; printf("%d", *ptr); will lead to undefined behavior.
  1. Not accounting for array bounds: When traversing an array using pointers, always remember to check the array's bounds to avoid accessing memory outside of its allocated space. This can lead to segmentation faults or unpredictable results.

Practice Questions

  1. Write a function that takes an array of integers and returns the sum of all its elements using pointer arithmetics.
  2. Given the following code snippet, what will be the output?
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0];
ptr += 2;
printf("%d", *ptr);
  1. Write a function that reverses an array of integers using pointer arithmetics and no temporary variables.

FAQ

  1. What happens if I increment a pointer beyond the end of an array? Incrementing a pointer beyond the end of an array leads to undefined behavior, as you will be accessing memory outside of its allocated space. This can result in segmentation faults or unpredictable results.
  1. Can I decrement a pointer and use it to traverse an array from the end towards the beginning? Yes! You can decrement a pointer and use it to traverse an array from the end towards the beginning by simply using the -- operator instead of ++.
  1. Is it possible to have a null pointer (a pointer that doesn't point to any memory)? Yes, in C, a null pointer is represented by the constant NULL, which is defined as an integer equal to 0 in the standard library header file stddef.h. A null pointer does not point to any valid memory address and should be treated carefully when dereferencing or performing arithmetic operations on it.