C program to swap two arrays using pointers

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

C program to swap two arrays using pointers. Explore how to efficiently swap two arrays in C programming using pointers. This article provides step-by-step guidance and a complete code example.

Swapping elements between arrays is a common operation in programming, often used for reordering data or performing transformations. In the C programming language, array swapping can be efficiently achieved using pointers, which allow direct manipulation of memory addresses. By understanding how to swap arrays using pointers, programmers can write more concise and optimized code.

In this article, we'll explore how to swap two arrays using pointers in C. We'll discuss the underlying principles and provide a step-by-step guide along with a complete code example to demonstrate the process.

Understanding Pointers and Arrays in C

Before diving into array swapping, let's review the basics of pointers and arrays in C:

  • Arrays: Arrays in C are collections of elements of the same data type stored in contiguous memory locations. They provide a convenient way to store and access a fixed-size sequence of elements.
  • Pointers: Pointers are variables that store memory addresses. They enable direct access to the memory locations where data is stored, allowing for efficient manipulation and traversal of data structures.

Swapping Arrays using Pointers

To swap two arrays in C using pointers, we'll utilize pointers to iterate through the elements of both arrays and perform the swapping operation. Here's a step-by-step guide:

for (int i = 0; i < SIZE; i++) {
    int temp = *(srcPtr + i);
    *(srcPtr + i) = *(destPtr + i);
    *(destPtr + i) = temp;
}

Write a C program to swap two arrays using pointers

#include <stdio.h>

#define MAX_SIZE 100

int main() {
    int sourceArray[MAX_SIZE];
    int destinationArray[MAX_SIZE];
    int *srcPtr = sourceArray;
    int *destPtr = destinationArray;
    int SIZE;

    printf("Enter size of array: ");
    scanf("%d", &SIZE);

    printf("Enter source array elements -\n");
    for (int i = 0; i < SIZE; i++) {
        scanf("%d", (sourceArray+i));
    }

    printf("Enter destination array elements -\n");
    for (int i = 0; i < SIZE; i++) {
        scanf("%d", (destinationArray+i));
    }

    // Swap elements using pointers
    for (int i = 0; i < SIZE; i++) {
        int temp = *(srcPtr + i);
        *(srcPtr + i) = *(destPtr + i);
        *(destPtr + i) = temp;
    }

    // Display swapped arrays
    printf("Swapped Source Array: ");
    for (int i = 0; i < SIZE; i++) {
        printf("%d ", sourceArray[i]);
    }
    printf("\n");

    printf("Swapped Destination Array: ");
    for (int i = 0; i < SIZE; i++) {
        printf("%d ", destinationArray[i]);
    }
    printf("\n");

    return 0;
}

Output

Enter size of array: 3
Enter source array elements -
3
2
1
Enter destination array elements -
9
8
7
Swapped Source Array: 9 8 7
Swapped Destination Array: 3 2 1

Conclusion

Swapping arrays using pointers in C offers a powerful and efficient method for reordering data. By leveraging pointers to access and manipulate array elements directly in memory, programmers can achieve array swapping with minimal overhead. Understanding the principles outlined in this article empowers developers to write concise and optimized code for array manipulation tasks in C. Practice and experimentation are key to mastering these concepts, so dive in and start swapping arrays with pointers!