Pointers with Arrays in C
In C, pointers provide an efficient way to work with arrays by directly accessing and manipulating memory locations. Using pointers with arrays allows for dynamic data manipulation, iteration, and improved performance in various operations.
In this tutorial, we will guide you through different ways to use pointers with arrays using practical examples.
Examples of Using Pointers with Arrays
1. Accessing Array Elements Using Pointers
In this example, we will use a pointer to access and print the elements of an integer array.
main.c
#include <stdio.h>
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int *ptr = numbers; // Pointer to the first element of the array
// Using pointer to access array elements
for (int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}
return 0;
}
Explanation:
- We declare an integer array
numbers[]
containing 5 elements. - We initialize a pointer
ptr
to the first element of the array usingint *ptr = numbers;
. - The
for
loop iterates through the array, incrementing the pointer offset*(ptr + i)
to access each element. - The
printf()
function prints each value accessed using the pointer.
Output:
10 20 30 40 50
2. Modifying Array Elements Using Pointers
In this example, we will use a pointer to modify the elements of an integer array.
main.c
#include <stdio.h>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int *ptr = numbers; // Pointer to array
// Modifying array elements using pointer
for (int i = 0; i < 5; i++) {
*(ptr + i) *= 2; // Doubling each element
}
// Printing modified array
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Explanation:
- We declare an integer array
numbers[]
with initial values. - We define a pointer
ptr
to point to the first element of the array. - The first
for
loop modifies each element by multiplying it by 2 using*(ptr + i) *= 2;
. - The second
for
loop prints the updated array.
Output:
2 4 6 8 10
3. Pointer Arithmetic with Arrays
In this example, we will use pointer arithmetic to navigate through an array and print its elements in reverse order.
main.c
#include <stdio.h>
int main() {
int numbers[] = {5, 10, 15, 20, 25};
int *ptr = numbers + 4; // Pointing to the last element
// Printing array elements in reverse order using pointer arithmetic
for (int i = 0; i < 5; i++) {
printf("%d ", *ptr);
ptr--; // Move pointer backward
}
return 0;
}
Explanation:
- We declare an integer array
numbers[]
with 5 elements. - We initialize a pointer
ptr
to the last element usingnumbers + 4
. - The
for
loop prints each element using*ptr
and decrements the pointer to move backward. - This continues until all elements are printed in reverse order.
Output:
25 20 15 10 5
Conclusion
In this tutorial, we explored how to use pointers with arrays in C with various examples:
- Accessing array elements using pointers: Demonstrates how to use a pointer to traverse an array.
- Modifying array elements using pointers: Shows how pointers can be used to update array values.
- Pointer arithmetic with arrays: Explains how pointers can be incremented and decremented to navigate arrays.