C Pointer Arithmetic

C Tutorial

 C Pointer Arithmetic (Beginner → Advanced)

Pointer arithmetic in C language allows you to move through memory locations using pointers.
It is a core concept for arrays, strings, dynamic memory, DSA, and interviews.


 What is Pointer Arithmetic?

It means performing arithmetic operations on pointers to access different memory locations.

 Pointer arithmetic is scaled by data type size, not by bytes directly.


 Allowed Pointer Operations in C

  • pointer + integer 
  • pointer - integer
  •  pointer++ / pointer--
  •  pointer1 - pointer2
  •  pointer + pointer (NOT allowed)

 Pointer++ (p++)


 

  • p++ moves pointer by sizeof(int) bytes (usually 4 bytes)

 Pointer– (p--)


 


 Pointer + Integer


 

  • arr + 2 → address of arr[2]

Pointer – Integer

  •  Moves pointer backward

 Pointer Difference (p1 - p2)


 

  •  Result = number of elements, not bytes

 Pointer Arithmetic with Arrays (Most Important)

  •  Array indexing internally uses pointer arithmetic
  • arr[i] == *(arr + i)

 Pointer Arithmetic with Characters (Strings)


 

  •  Moves 1 byte at a time

 Pointer Arithmetic with Structures


 

  •  Moves by sizeof(struct Student)

 Pointer Arithmetic with Dynamic Memory


 


 Illegal Pointer Arithmetic


 Common Mistakes

  •  Forgetting pointer type size
  •  Accessing out-of-bounds memory
  •  Confusing pointer arithmetic with integer arithmetic
  •  Using uninitialized pointers

 Interview Questions (Must Prepare)

  1. Why does p++ move by size of data type?

  2. Difference between arr[i] and *(arr+i)

  3. What does p1 - p2 return?

  4. Is pointer + pointer allowed?

  5. Pointer arithmetic on void* allowed?


 Real-Life Use Cases

  • Array traversal

  • String processing

  • Memory buffers

  • Embedded systems

  • Data structures (Linked List, Stack)


 Summary

  •  Pointer arithmetic works in units of data type size 
  •  Very efficient for arrays & strings
  • arr[i] internally uses pointer arithmetic
  •  Critical for DSA, OS, Embedded & Interviews

You may also like...