Back to C Programming
2026-07-146 min read

C - Pointer Arithmetics

Learn C - Pointer Arithmetics step by step with clear examples and exercises.

Why This Matters

In this full guide on C Pointer Arithmetics, we aim to provide a deep understanding of pointers, their arithmetic operations, and common pitfalls that developers often encounter. This lesson covers various aspects such as the importance of pointer arithmetics, prerequisites, core concepts, worked examples, common mistakes, practice questions, and frequently asked questions.

Why This Matters

Pointer arithmetics are crucial in C programming as they enable direct manipulation of memory locations, which is essential for dynamic memory allocation, functions, and data structures like arrays and linked lists. Mastering pointer arithmetics can help you write more efficient code, solve complex problems, and prepare for job interviews or exams that test your C programming skills.

Prerequisites

Before delving into pointer arithmetics, it is essential to have a strong grasp of the following concepts:

  1. Variables and data types
  2. Arrays
  3. Pointers (basic concept)
  4. Memory management in C
  5. Basic input/output operations using printf and scanf
  6. Structures and pointers to structures
  7. Dynamic memory allocation using malloc, calloc, realloc, and free
  8. Pointers to functions

Core Concept

What are pointers?

In C, a pointer is a variable that stores the memory address of another variable. Pointers allow you to access and manipulate the data stored at specific memory locations.

Pointer variables

To declare a pointer variable, use the * symbol before the variable name:

int num = 10;
int *ptr; // Declare a pointer to an integer

Pointing a pointer

To assign a memory address to a pointer, use the & operator:

ptr = # // Assign the memory address of num to ptr

Now, ptr points to the memory location where num is stored.

Pointer arithmetics

You can perform arithmetic operations on pointers to move between memory locations:

  1. Increment/decrement a pointer (ptr++, ptr--)
  2. Add or subtract an integer to/from a pointer (ptr += 3, ptr -= 4)
  3. Calculate the difference between two pointers (ptr2 - ptr1)

Here's an example of pointer arithmetics:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // Initialize ptr to the first element of arr

// Increment ptr to point to the second element
ptr++;
printf("%d\n", *ptr); // Output: 2

Dereferencing a pointer

To access the data stored at the memory location pointed by a pointer, use the * operator:

printf("%d\n", *ptr); // Access the data stored at the memory location pointed by ptr

Pointer arithmetics with arrays and structures

Pointer arithmetics work seamlessly with arrays and structures. For example, to traverse an array or access elements of a structure, you can increment the pointer:

Array Example:

int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // Initialize ptr to the first element of arr
for (int i = 0; i < 5; i++) {
printf("%d\n", *ptr);
ptr++; // Move ptr to the next element
}

Structure Example:

typedef struct {
int id;
char name[20];
float salary;
} Employee;

Employee employees[3] = {
{1, "John Doe", 50000.0},
{2, "Jane Smith", 60000.0},
{3, "Mike Johnson", 70000.0}
};

Employee *ptr = &employees[0]; // Initialize ptr to the first employee
for (int i = 0; i < 3; i++) {
printf("ID: %d, Name: %s, Salary: %.2f\n", ptr->id, ptr->name, ptr->salary);
ptr++; // Move ptr to the next employee
}

Worked Example

Let's create a simple program that demonstrates pointer arithmetics using an array and calculating the sum of all elements. We will also use dynamic memory allocation to create a larger array and compare the performance of pointer arithmetics with a for loop.

#include <stdio.h>
#include <stdlib.h>

int main() {
int *arr; // Declare an integer pointer
int n, sum = 0;

printf("Enter the number of elements: ");
scanf("%d", &n);

// Dynamically allocate memory for the array
arr = (int *)malloc(n * sizeof(int));
if (!arr) {
printf("Memory allocation failed!\n");
return 1;
}

printf("Enter %d integers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", arr + i); // Use pointer arithmetics to read the input
sum += *(arr + i); // Use pointer arithmetics to calculate the sum
}

// Free the allocated memory
free(arr);

printf("The sum of all elements is: %d\n", sum);
return 0;
}

When you run this program, it will output the sum of the entered integers. This example demonstrates the efficiency of pointer arithmetics when dealing with dynamic memory allocation.

Common Mistakes

  1. Forgetting to initialize a pointer before using it:
int *ptr; // Declare an uninitialized pointer
printf("%d\n", *ptr); // Output undefined behavior
  1. Accessing memory locations beyond the allocated area:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[5]; // Point ptr to an invalid memory location
printf("%d\n", *ptr); // Output undefined behavior
  1. Incrementing a pointer beyond the end of the array:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // Initialize ptr to the first element of arr
for (int i = 0; i < 6; i++) { // Increment ptr beyond the end of the array
printf("%d\n", *ptr);
ptr++;
}

This program will output 1, 2, 3, 4, 5, garbage value. It's essential to check if a pointer is within the bounds of an array before dereferencing it.

Common Mistakes (Contd.)

  1. Using incorrect pointer arithmetic operations:
int arr[5] = {1, 2, 3, 4, 5};
int *ptr = &arr[0]; // Initialize ptr to the first element of arr
ptr++; // Move ptr to the second element (correct)
ptr + 1; // Also moves ptr to the second element (equivalent but clearer)
*(ptr + 1); // Accesses the data stored at the memory location pointed by ptr+1 (incorrect)
  1. Forgetting to deallocate dynamically allocated memory:
int *arr; // Declare an integer pointer
int n, sum = 0;

printf("Enter the number of elements: ");
scanf("%d", &n);

// Dynamically allocate memory for the array
arr = (int *)malloc(n * sizeof(int));
if (!arr) {
printf("Memory allocation failed!\n");
return 1;
}

// ... (process the input and calculate the sum)

// Forget to free the allocated memory
// ... (further processing or returning from main)

This program will leak memory when it terminates, causing potential issues in future runs. Always remember to deallocate dynamically allocated memory using free().

Practice Questions

  1. Write a function that swaps two integers using pointers.
  2. Create a program that finds the second-highest number in an array using pointer arithmetics and without using additional arrays or sorting functions.
  3. Implement a function that reverses an array of characters using pointers.
  4. Write a program that sorts an array of integers using bubble sort with pointer arithmetics.
  5. Create a program that implements a simple linked list using pointers and dynamically allocates memory for each node.

FAQ

What happens if I access an invalid memory location using a pointer?

Accessing an invalid memory location using a pointer can lead to undefined behavior, such as segmentation faults or memory corruption.

Can I use pointer arithmetics with arrays of structures?

Yes! Pointer arithmetics work with arrays of structures as well. You can move between the elements of an array of structures by incrementing the pointer.

How do I check if a pointer is pointing to a valid memory location within an array?

To check if a pointer is pointing to a valid memory location within an array, you can compare it with the address of the last element in the array plus one: ptr >= arr && ptr < (arr + sizeof(arr) / sizeof(*arr)).

How do I deallocate dynamically allocated memory for arrays of structures or custom data types?

To deallocate dynamically allocated memory for arrays of structures or custom data types, you should first iterate through the array and free each element individually before freeing the array itself:

typedef struct {
// Custom data type definition
} MyType;

MyType *arr = (MyType *)malloc(n * sizeof(MyType));
for (int i = 0; i < n; i++) {
free(arr + i); // Free each element individually
}
free(arr); // Free the array itself