---
title: "Arithmetic operations on pointers"
subject: "Computer Programming"
chapter: 9
key: "semester_1/computer_programming:pointer-arithmetic-operations"
source: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-arithmetic-operations
site: BCT Engineering
---

# Arithmetic operations on pointers

Computer Programming · Chapter 9: Pointers

- Asked in: 2069 Asar, 3 marks
- Video: [Master Pointer Arithmetic In 10 Minutes (The Ultimate Guide For C Programmers).](https://www.youtube.com/watch?v=ocOFKb6V6XE) — HuwsTube
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/pointer-arithmetic-operations
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-9

## Question

What type of arithmetic operations can be implemented in pointer? Describe with appropriate syntax and examples.

## Short answer

Allowed: p++, p--, p+n, p-n, q-p (element count), comparisons. Not allowed: addition of two pointers, multiplication, division, float addition. Example: int a[5], *p=a; p++; p+2; q-p gives element difference.

## Answer

The following arithmetic operations are allowed on pointers:

| Operation | Syntax | Meaning |
|---|---|---|
| Increment / decrement | `p++`, `p--` | Moves to the next / previous element (by `sizeof` the type) |
| Add an integer | `p + n` | Address of the n-th element after p |
| Subtract an integer | `p - n` | Address of the n-th element before p |
| Subtract two pointers | `q - p` | Number of elements between them (same array only) |
| Comparison | `p < q`, `p == q` | Compares addresses (same array) |

Not allowed: adding two pointers, multiplying or dividing a pointer, or adding a float.

```c
#include <stdio.h>

int main(void)
{
    int a[5] = {10, 20, 30, 40, 50};
    int *p = a, *q = &a[4];

    printf("*p = %d\n", *p);
    p++;                                   /* next element */
    printf("after p++      : *p = %d\n", *p);
    p = p + 2;
    printf("after p = p + 2: *p = %d\n", *p);
    p--;
    printf("after p--      : *p = %d\n", *p);
    printf("q - p = %ld elements\n", (long)(q - p));
    printf("p < q is %d\n", p < q);
    return 0;
}
```

Output:

```text
*p = 10
after p++      : *p = 20
after p = p + 2: *p = 40
after p--      : *p = 30
q - p = 2 elements
p < q is 1
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which operation on two pointers into the same array is allowed?
   A. p + q
   B. p * q
   C. q - p
   D. p / q
   Answer: C. Subtracting two pointers gives the number of elements between them; adding, multiplying or dividing pointers is not allowed. (Key point: Valid pointer operations)

2. With int a[5] and p = a, q = &a[4], what is q - p?
   A. 16
   B. 4
   C. 5
   D. 1
   Answer: B. Pointer subtraction counts elements, not bytes, so the difference is 4 elements. (Key point: Result of pointer subtraction)

3. If p points to a[1] of {10,20,30,40,50}, what does *p show after p = p + 2?
   A. 30
   B. 40
   C. 50
   D. 20
   Answer: B. p moves two elements ahead from a[1] to a[3], which holds 40. (Key point: Effect of p = p + 2)

## Related in Computer Programming

- [Pointers in pass by reference; 2D array](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-passing-by-reference-2d) — Computer Programming, chapter 9
- [Accessing array elements using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-access-array-elements) — Computer Programming, chapter 9
- [Swap two values using pointers](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-swap-function) — Computer Programming, chapter 9
