Pointers in C support arithmetic operations, which allows you to perform calculations involving memory addresses. Pointer arithmetic is typically used for traversing arrays or manipulating data in contiguous memory locations. The primary arithmetic operations that can be performed with pointers are increment (++
), decrement (--
), addition (+
), and subtraction (-
).
Important Note About Pointer Arithmetic
- Pointer arithmetic depends on the size of the data type.
- When you perform arithmetic operations on pointers, the pointer moves by the size of the data type it points to.
- For example, if a pointer points to an
int
and you increment it by1
, the pointer will move bysizeof(int)
bytes.
Let’s go over each type of pointer arithmetic operation:
1. Increment and Decrement Operators (++
, --
)
- Increment (
++
): Moves the pointer to the next element. - Decrement (
--
): Moves the pointer to the previous element.
Example
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40};
int *p = arr; // Points to the first element of the array (arr[0])
printf("Current value: %d\n", *p); // Output: 10
p++; // Moves the pointer to the next element in the array (arr[1])
printf("After increment: %d\n", *p); // Output: 20
p--; // Moves the pointer back to the previous element (arr[0])
printf("After decrement: %d\n", *p); // Output: 10
return 0;
}
- Initially,
p
points toarr[0]
. - After
p++
,p
moves toarr[1]
. - After
p--
,p
moves back toarr[0]
.
2. Addition and Subtraction (+
, -
)
- Addition (
+
): Adds an integer value to a pointer, moving it forward by a certain number of elements. - Subtraction (
-
): Subtracts an integer value from a pointer, moving it backward by a certain number of elements.
Example
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *p = arr; // Points to the first element (arr[0])
p = p + 3; // Moves the pointer forward by 3 elements (points to arr[3])
printf("Value after addition: %d\n", *p); // Output: 40
p = p - 2; // Moves the pointer back by 2 elements (points to arr[1])
printf("Value after subtraction: %d\n", *p); // Output: 20
return 0;
}
p + 3
moves the pointer forward by3
elements.p - 2
moves it backward by2
elements.
3. Pointer Difference
- You can subtract one pointer from another if both pointers point to elements of the same array. The result is the number of elements between the two pointers.
Example
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *p1 = &arr[1]; // Points to the second element (arr[1])
int *p2 = &arr[4]; // Points to the fifth element (arr[4])
int difference = p2 - p1; // Calculates the number of elements between p1 and p2
printf("Difference between pointers: %d\n", difference); // Output: 3
return 0;
}
p2 - p1
gives the difference in terms of the number of elements, which is3
.
Pointer Arithmetic Rules
- Type Matters: When you increment or add to a pointer, the pointer moves by the size of the type it points to. For example:
- If
p
is anint *
andsizeof(int)
is4
bytes, thenp++
movesp
by4
bytes. - If
p
is achar *
andsizeof(char)
is1
byte, thenp++
movesp
by1
byte.
- If
- Pointer Comparison: You can compare pointers using relational operators (
==
,!=
,<
,>
, etc.), particularly useful when traversing arrays to check boundaries.
Example Demonstrating Different Pointer Arithmetic Operations
#include <stdio.h>
int main() {
int arr[] = {5, 10, 15, 20, 25};
int *p = arr; // Points to arr[0]
// Print values using pointer arithmetic
for (int i = 0; i < 5; i++) {
printf("Element %d: %d\n", i, *(p + i)); // Outputs each element of the array
}
// Moving the pointer manually
p = arr; // Reset p to the start of the array
p += 2; // Now points to arr[2]
printf("Value at p after p += 2: %d\n", *p); // Output: 15
p -= 1; // Now points to arr[1]
printf("Value at p after p -= 1: %d\n", *p); // Output: 10
return 0;
}
Summary
- Increment (
p++
) and Decrement (p--
): Move the pointer to the next or previous element. - Addition (
p + n
) and Subtraction (p - n
): Move the pointer forward or backward byn
elements. - Pointer Difference (
p2 - p1
): Calculates the number of elements between two pointers. - Pointer arithmetic is meaningful only when pointers point to elements of the same array or contiguous memory.
Pointer arithmetic allows efficient navigation through arrays and dynamic memory, enabling you to directly manipulate memory locations and improve performance when working with collections of data. However, incorrect pointer arithmetic can lead to undefined behavior, such as accessing memory outside the allocated bounds, so it must be used carefully.