C Pointer Arithmetic

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 + integerpointer - integerpointer++/pointer--pointer1 - pointer2pointer + pointer(NOT allowed)
Pointer++ (p++)
p++moves pointer bysizeof(int)bytes (usually 4 bytes)
Pointer– (p--)
Pointer + Integer
arr + 2→ address ofarr[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)
Why does
p++move by size of data type?Difference between
arr[i]and*(arr+i)What does
p1 - p2return?Is
pointer + pointerallowed?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
