C program to reverse an array using pointers

Category: C Program
Tags: #cprogram#pointers#array#reverse

Discover how to efficiently reverse an array in C using call by reference and print its elements separately. Explore step-by-step guidance and complete code examples for mastering array manipulation with pointers in C programming.

Reversing an array is a common operation in programming, particularly when dealing with arrays in C. Employing pointers, we can efficiently reverse the elements of an array, offering a dynamic and optimized approach to array manipulation.

In this article, we'll delve into how to reverse an array using pointers in C. Understanding this concept is crucial for mastering array manipulation and memory management in the C programming language.

Understanding Pointers and Arrays in C

Before we dive into reversing arrays using pointers, let's grasp the basics of pointers and arrays in C:

  • Arrays: Arrays in C are contiguous blocks of memory that store elements of the same data type. They offer a convenient way to organize and manipulate a collection of elements.
  • Pointers: Pointers in C are variables that store memory addresses. They enable direct access to memory locations, allowing for efficient memory management and manipulation of data.

Reversing Arrays using Pointers

To reverse an array in C using pointers, we leverage pointer arithmetic to swap elements from the beginning and end of the array until we reach the middle. Here's a step-by-step guide to reversing arrays using pointers:

  1. Declare Array and Pointers: Define an array and pointers to the first and last elements of the array.

    #include <stdio.h>
    
    #define SIZE 10
    
    int main() {
        int arr[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int *startPtr = arr;
        int *endPtr = arr + SIZE - 1;
    
  2. Swap Elements using Pointers: Iterate through the array, swapping elements pointed to by startPtr and endPtr until they meet or cross each other.

     while (startPtr < endPtr) {
         int temp = *startPtr;
         *startPtr = *endPtr;
         *endPtr = temp;
         startPtr++;
         endPtr--;
     }
    
  3. Display Reversed Array (Optional): If desired, display the reversed array to verify the reversal operation.

     printf("Reversed Array: ");
     for (int i = 0; i < SIZE; i++) {
         printf("%d ", arr[i]);
     }
    

Write a C program to reverse an array using pointers

Putting it all together, here's the complete C program to reverse an array using pointers:

#include <stdio.h>

#define SIZE 10

int main() {
    int arr[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int *startPtr = arr;
    int *endPtr = arr + SIZE - 1;

    // Reverse the array using pointers
    while (startPtr < endPtr) {
        int temp = *startPtr;
        *startPtr = *endPtr;
        *endPtr = temp;
        startPtr++;
        endPtr--;
    }

    // Display the reversed array
    printf("Reversed Array: ");
    for (int i = 0; i < SIZE; i++) {
        printf("%d ", arr[i]);
    }

    return 0;
}

When working with pointers it is a common way to pass data by reference and manipulate it so that we can follow a modular approach and create reusable functions.

#include <stdio.h>

#define SIZE 10

// Function to reverse the array using call by reference
void reverseArray(int *arr, int size) {
    int *startPtr = arr;
    int *endPtr = arr + size - 1;

    // Reverse the array using pointers
    while (startPtr < endPtr) {
        int temp = *startPtr;
        *startPtr = *endPtr;
        *endPtr = temp;
        startPtr++;
        endPtr--;
    }
}

// Function to print the array
void printArray(int *arr, int size) {
    printf("Array: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
}

int main() {
    int arr[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // Reverse the array using call by reference
    reverseArray(arr, SIZE);

    // Print the reversed array
    printArray(arr, SIZE);

    return 0;
}

In this code:

  • The reverseArray function takes an array and its size as arguments and reverses the array using pointers.
  • The printArray function takes an array and its size as arguments and prints the elements of the array.

Output

Reversed Array: 10 9 8 7 6 5 4 3 2 1