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[sourceArrSize - 1]; and *destArrEnd = &destArray[destArrSize - 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 sourceArrSize, int *destArr, int destArrSize);




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

    int sourceArrSize, destArrSize;


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

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


    // Input size of destination array
    printf("\nEnter size of destination array: ");
    scanf("%d", &destArrSize);

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



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

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


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


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

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

    
    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.
 * @sourceArrSize   Size of source array.
 * @destArr         Pointer to destination array to swap.
 * @destArrSize     Size of destination array.
 */
void swapArray(int * sourceArr, int sourceArrSize, int * destArr, int destArrSize)
{
    // Pointer to last element of source array
    int * sourceArrEnd = (sourceArr + (sourceArrSize - 1));

    // Pointer to last element of destination array
    int * destArrEnd   = (destArr + (destArrSize - 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 source array: 10
Enter elements in source array: 10 20 30 40 50 60 70 80 90 100

Enter size of destination array: 10
Enter elements in destination array: 9 8 7 6 5 4 3 2 1 0


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

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

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>