Day 2: Pointer Arithmetic in C — A Detailed Guide
By the end of this lesson, you will understand how pointers can be manipulated using arithmetic operations, how pointer arithmetic differs…
Day 2: Pointer Arithmetic in C — A Detailed Guide
By the end of this lesson, you will understand how pointers can be manipulated using arithmetic operations, how pointer arithmetic differs from normal arithmetic, and how to use pointers to efficiently traverse arrays and data structures.
1. Introduction to Pointer Arithmetic
A pointer stores the memory address of a variable. Since memory locations are arranged sequentially, C allows arithmetic operations on pointers to move through memory locations.
Unlike ordinary integers, pointer arithmetic takes the size of the data type into account.
Consider:
int x = 10;
int *ptr = &x;
Suppose ptr stores address 1000.
Since an integer occupies 4 bytes on most systems:
ptr + 1
does not become 1001.
Instead it becomes:
1000 + 4 = 1004
because the pointer moves to the next integer location.
2. Why Pointer Arithmetic Exists
Pointer arithmetic allows efficient traversal of:
- Arrays
- Strings
- Dynamic memory
- Data structures
- Buffers
Instead of maintaining an index, we can simply move the pointer.
Example:
int arr[5] = {10,20,30,40,50};
int *ptr = arr;
printf("%d\n", *ptr);
ptr++;
printf("%d\n", *ptr);
Output:
10
20
The pointer moved from the first element to the second.
3. Incrementing Pointers
The increment operator (++) moves a pointer to the next memory location of its type.
Example
#include <stdio.h>
int main()
{
int arr[] = {10,20,30};
int *ptr = arr;
printf("%d\n", *ptr);
ptr++;
printf("%d\n", *ptr);
return 0;
}
Output:
10
20
Memory Representation
Assume:
Address Value
1000 10
1004 20
1008 30
Initially:
ptr = 1000
After:
ptr++;
ptr = 1004
4. Decrementing Pointers
The decrement operator (--) moves a pointer to the previous memory location.
Example:
int arr[] = {10,20,30};
int *ptr = &arr[2];
printf("%d\n", *ptr);
ptr--;
printf("%d\n", *ptr);
Output:
30
20
5. Pointer Addition
We can add an integer value to a pointer.
Syntax:
pointer + n
This moves the pointer forward by n elements.
Example:
int arr[] = {10,20,30,40,50};
int *ptr = arr;
printf("%d\n", *(ptr + 3));
Output:
40
Explanation:
ptr + 3
moves three integer positions ahead.
6. Pointer Subtraction
Pointers can also move backward.
Example:
int arr[] = {10,20,30,40,50};
int *ptr = &arr[4];
printf("%d\n", *(ptr - 2));
Output:
30
7. Pointer Difference
Subtracting two pointers gives the number of elements between them.
Example:
int arr[] = {10,20,30,40,50};
int *p1 = &arr[4];
int *p2 = &arr[1];
printf("%ld\n", p1 - p2);
Output:
3
Why 3?
arr[4] - arr[1]
means:
50 - 20
There are three integer positions between them.
Important:
Pointer subtraction returns element count, not byte count.
8. Illegal Pointer Operations
Not all arithmetic operations are allowed.
Valid
ptr++
ptr--
ptr + n
ptr - n
ptr1 - ptr2
Invalid
ptr * 2
ptr / 2
ptr % 2
ptr + ptr
Example:
int *ptr;
ptr * 2;
Compiler Error.
Pointers can only be moved, not multiplied or divided.
9. Pointer Arithmetic with Different Data Types
The amount by which a pointer moves depends on the data type size.
Integer Pointer
int *ptr;
ptr++;
Moves:
sizeof(int) bytes
Usually:
4 bytes
Character Pointer
char *ptr;
ptr++;
Moves:
1 byte
because a character occupies one byte.
Double Pointer
double *ptr;
ptr++;
Moves:
8 bytes
on most systems.
Demonstration
#include <stdio.h>
int main()
{
int a;
char b;
double c;
printf("%zu\n", sizeof(a));
printf("%zu\n", sizeof(b));
printf("%zu\n", sizeof(c));
return 0;
}
Possible Output:
4
1
8
Pointer arithmetic automatically uses these sizes.
10. Difference Between Pointer Arithmetic and Integer Arithmetic
Integer Arithmetic
int x = 1000;
x = x + 1;
Result:
1001
Only one unit added.
Pointer Arithmetic
int *ptr = (int *)1000;
ptr = ptr + 1;
Result:
1004
because:
1000 + sizeof(int)
11. Arrays and Pointer Arithmetic
Array names behave like constant pointers.
Example:
int arr[] = {10,20,30,40,50};
The following are equivalent:
arr[2]
and
*(arr + 2)
Output:
30
Example:
printf("%d\n", arr[2]);
printf("%d\n", *(arr + 2));
Both print:
30
12. Traversing Arrays Using Pointers
Traditional Method:
for(int i=0;i<5;i++)
{
printf("%d ", arr[i]);
}
Pointer Method:
int *ptr = arr;
for(int i=0;i<5;i++)
{
printf("%d ", *ptr);
ptr++;
}
Output:
10 20 30 40 50
13. Pointer Comparisons
Pointers can be compared when they point into the same array.
Example:
int arr[5];
int *p1 = &arr[1];
int *p2 = &arr[3];
if(p1 < p2)
{
printf("p1 comes before p2");
}
Output:
p1 comes before p2
Valid comparison operators:
<
>
<=
>=
==
!=
14. Typecasting Pointers
Sometimes a pointer is converted into another pointer type.
Example:
int num = 65;
char *ptr = (char *)#
Now:
printf("%c\n", *ptr);
may print:
A
depending on system architecture.
Why Typecasting?
- Access raw memory
- Work with binary files
- Generic programming
- Embedded systems
15. Important Rules of Pointer Arithmetic
Rule 1
Adding 1 moves to the next object.
ptr++;
Rule 2
Movement depends on data type size.
char *
moves 1 byte.
int *
moves 4 bytes (typically).
Rule 3
Subtracting pointers gives element distance.
p2 - p1
returns number of elements.
Rule 4
Pointers must belong to the same array for subtraction and comparison.
Rule 5
Never move beyond array boundaries.
Bad:
int arr[5];
int *ptr = arr + 10;
This causes undefined behavior.
Hands-On Exercise: Navigate an Array Using Pointers
#include <stdio.h>
int main()
{
int arr[] = {5,10,15,20,25};
int *ptr = arr;
printf("Forward Traversal:\n");
for(int i=0;i<5;i++)
{
printf("%d ", *ptr);
ptr++;
}
return 0;
}
Output:
Forward Traversal:
5 10 15 20 25
Mini Project: Pointer-Based Array Reversal
Problem
Reverse an array using pointers instead of indexing.
Algorithm
- Create two pointers.
- One points to the first element.
- One points to the last element.
- Swap values.
- Move pointers inward.
- Repeat until pointers meet.
Program
#include <stdio.h>
void reverseArray(int *start, int *end)
{
while(start < end)
{
int temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main()
{
int arr[] = {1,2,3,4,5};
int size = sizeof(arr)/sizeof(arr[0]);
reverseArray(arr, arr + size - 1);
printf("Reversed Array:\n");
for(int i=0;i<size;i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Output:
Reversed Array:
5 4 3 2 1
Common Mistakes Beginners Make
Mistake 1
Dereferencing an uninitialized pointer.
int *ptr;
printf("%d", *ptr);
Undefined behavior.
Mistake 2
Moving beyond array boundaries.
ptr++;
after the last element.
Mistake 3
Confusing pointer value with pointed value.
ptr
is an address.
*ptr
is data.
Mistake 4
Subtracting unrelated pointers.
p1 - p2
when they belong to different arrays.
Undefined behavior.
Day 2 Summary
In this lesson, you learned:
- What pointer arithmetic is
- Incrementing and decrementing pointers
- Pointer addition and subtraction
- Difference between pointer arithmetic and integer arithmetic
- Pointer subtraction and comparisons
- Typecasting pointers
- Array traversal using pointers
- Reversing arrays using pointer techniques
- Common pointer-related mistakes
Pointer arithmetic is one of the most powerful features of C because it provides direct control over memory and enables efficient implementation of arrays, strings, dynamic memory structures, and advanced data structures. Mastering these concepts is essential before moving on to the deeper relationship between pointers and arrays in Day 3.
메타데이터
- post_id
- 2e3ba6329c2c
- slug
- day-2-pointer-arithmetic-in-c-a-detailed-guide-2e3ba6329c2c
- url
- https://medium.com/@tabspacepresentations/day-2-pointer-arithmetic-in-c-a-detailed-guide-2e3ba6329c2c
- canonical_url
- https://medium.com/@tabspacepresentations/day-2-pointer-arithmetic-in-c-a-detailed-guide-2e3ba6329c2c
- author_url
- https://medium.com/@tabspacepresentations
- status
- ok
- fetched_at
- 2026-07-14 20:51:12