C program to swap two arrays using pointers

Write a C program to swap corresponding elements of two arrays using pointers. How to swap two arrays using pointers in C program. Logic to swap two arrays of different length using pointers in C programming.

Example

Input

Input first array: 10 20 30 40 50 60 70 80 90 100
Input second array: 0 9 8 7 6 5 4 3 2 1

Output

First array after swapping: 0 9 8 7 6 5 4 3 2 1
Second array after swapping:  10 20 30 40 50 60 70 80 90 100

Required knowledge

Basic C programming, Array, Pointers, Pointers and Array

Logic to swap two arrays using pointers

We have already seen how to swap two variables using pointers. Swapping arrays using pointers is similar. Below is the step by step descriptive logic to swap two arrays using pointers. The logic is same for same or different length of arrays.

  1. Input array elements in two arrays say sourceArray and destArray.
  2. Initialize a pointer to both arrays say *sourceArr = sourceArray and *destArr = destArray.
  3. Initialize two more pointers that keeps track of both array last memory address. Store them as *sourceArrEnd = &sourceArray[size - 1]; and *destArrEnd = &destArray[size – 1];.

    I will use these two pointers to ensure that we only traverse within memory occupied by two arrays.

  4. We will use bitwise XOR operator to swap two array elements. To swap two individual array elements perform
    *sourceArr  ^= *destArr;
    *destArr    ^= *sourceArr;
    *sourceArr  ^= *destArr;
  5. Increment sourceArr and destArr by 1.
  6. Repeat step 4 and 5 till sourceArr <= sourceArrEnd and destArr <= destArrEnd.

Program to swap two arrays using pointers

/**
 * C program to swap two arrays using pointers
 */

#include <stdio.h>

#define MAX_SIZE 100    // Maximum array size


/* Function declarations */
void inputArray(int *arr, int size);
void printArray(int *arr, int size);
void swapArray(int *sourceArr, int *destArr, int size);




int main()
{
    int sourceArr[MAX_SIZE];
    int destArr[MAX_SIZE];

    int size;


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

    // Input elements of destination array
    printf("Enter %d elements in source array: ", size);
    inputArray(sourceArr, size);


    // Input element of destination array
    printf("Enter %d elements in destination array: ", size);
    inputArray(destArr, size);


    /*
     * Print elements of both arrays before swapping
     */
    printf("\n\nSource array before swapping: ");
    printArray(sourceArr, size);

    printf("\nDestination array before swapping: ");
    printArray(destArr, size);


    /* Swap elements of both arrays. */
    swapArray(sourceArr, destArr, size);


    /*
     * Print elements of both arrays after swapping
     */
    printf("\n\nSource array after swapping: ");
    printArray(sourceArr, size);

    printf("\nDestination array after swapping: ");
    printArray(destArr, size);

    
    return 0;
}



/**
 * Function used to read input from user in an array.
 *
 * @arr     Pointer to array to store input
 * @size    Size of the array
 */
void inputArray(int *arr, int size)
{
    // Pointer to last element of array.
    int *arrEnd = (arr + (size - 1));


    // Input elements in array using pointer
    while(arr <= arrEnd)
        scanf("%d", arr++);
}



/**
 * Function used to print elements of array.
 * 
 * @arr     Pointer to array, which is to print.
 * @size    Size of the array
 */
void printArray(int *arr, int size)
{
    // Pointer to last element of array.
    int *arrEnd = (arr + (size - 1));


    // Print elements of array one by one
    while(arr <= arrEnd)
        printf("%d, ", *(arr++));
}



/**
 * Function to swap elements of two arrays.
 * 
 * @sourceArr       Pointer to source array to swap.
 * @destArr         Pointer to destination array to swap.
 * @size            Size of array.
 */
void swapArray(int * sourceArr, int * destArr, int size)
{
    // Pointer to last element of source array
    int * sourceArrEnd = (sourceArr + (size - 1));

    // Pointer to last element of destination array
    int * destArrEnd   = (destArr + (size - 1));


    /*
     * Swap individual element of both arrays
     */
    while(sourceArr <= sourceArrEnd && destArr <= destArrEnd)
    {
        *sourceArr ^= *destArr;
        *destArr   ^= *sourceArr;
        *sourceArr ^= *destArr;

        // Increment source array to point to next element
        sourceArr++;

        // Increment destination array to point to next element
        destArr++;
    }
}

Output

Enter size of array: 10
Enter 10 elements in source array: 1 2 3 4 5 6 7 8 9 10
Enter 10 elements in destination array: 10 20 30 40 50 60 70 80 90 100


Source array before swapping: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
Destination array before swapping: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,

Source array after swapping: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
Destination array after swapping: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10