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

</>
Copy
#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:

  1. We declare an integer array numbers[] containing 5 elements.
  2. We initialize a pointer ptr to the first element of the array using int *ptr = numbers;.
  3. The for loop iterates through the array, incrementing the pointer offset *(ptr + i) to access each element.
  4. 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

</>
Copy
#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:

  1. We declare an integer array numbers[] with initial values.
  2. We define a pointer ptr to point to the first element of the array.
  3. The first for loop modifies each element by multiplying it by 2 using *(ptr + i) *= 2;.
  4. 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

</>
Copy
#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:

  1. We declare an integer array numbers[] with 5 elements.
  2. We initialize a pointer ptr to the last element using numbers + 4.
  3. The for loop prints each element using *ptr and decrements the pointer to move backward.
  4. 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:

  1. Accessing array elements using pointers: Demonstrates how to use a pointer to traverse an array.
  2. Modifying array elements using pointers: Shows how pointers can be used to update array values.
  3. Pointer arithmetic with arrays: Explains how pointers can be incremented and decremented to navigate arrays.