Open In App

C Program to Copy an Array to Another Array

Last Updated : 21 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will learn how to copy all the elements of one array to another array in C.

The simplest method to copy an array is by using the memcpy() function. Let’s take a look at an example:

C
#include <stdio.h>
#include <string.h>

int main() {
    int arr1[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int arr2[n];

    // Use memcpy to copy arr1 to arr2
    memcpy(arr2, arr1, n * sizeof(arr1[0]));

    for (int i = 0; i < n; i++)
        printf("%d ", arr2[i]);
    return 0;
}

Output
1 2 3 4 5 

Explanation: The memcpy() function copied the whole block of arr1 memory to arr2.

This method operators directly on low level memory so it is fast but there are some chances of error if not properly used.

There is also a few more methods to copy all the elements of one array to another. Some of them are as follows:

Using a Loop

This method uses a loop to iterate through the array and assign each element to the corresponding index of another array.

C
#include <stdio.h>

void copyArr(int arr1[], int arr2[], int n) {
    for (int i = 0; i < n; i++) {

        // Copy each element one by one
        arr2[i] = arr1[i];
    }
}

int main() {
    int arr1[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int arr2[n];

  	// Copy arr1 to arr2
    copyArr(arr1, arr2, n);
  
    for (int i = 0; i < n; i++)
        printf("%d ", arr2[i]);
    return 0;
}

Output
1 2 3 4 5 

This method provides more control of the copying process.

Using Recursion

In this method, only one element is copied in one function call. The rest of the elements are then copied by recursively calling the function for next elements.

C
#include <stdio.h>

void copyArr(int arr1[], int arr2[], int n) {
    if (n == 0) return;
      
    // Copy the current element
    arr2[n - 1] = arr1[n - 1];
      
    // Copy the remaining array
    copyArr(arr1, arr2, n - 1);
}

int main() {
    int arr1[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int arr2[n];  

    // Copy elements of arr1 to arr2
    copyArr(arr1, arr2, n);

    for (int i = 0; i < n; i++)
        printf("%d ", arr2[i]);
    return 0;
}

Output
1 2 3 4 5 

This method is generally not preferred because it may take more memory as compared to other methods (if tail call optimization is not done).

Using Pointers

This method is similar to the loop method but here, pointers are used instead of array names.

C
#include <stdio.h>

void copyArr(int *arr1, int *arr2, int n) {
    for (int i = 0; i < n; i++) {

        // Copy each element by dereferencing
        *(arr2 + i) = *(arr1 + i);
    }
}

int main() {
    int arr1[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr1) / sizeof(arr1[0]);
    int arr2[n];

    // Copy arr1 to arr2
    copyArr(arr1, arr2, n);
  
    for (int i = 0; i < n; i++)
        printf("%d ", arr2[i]);

    return 0;
}

Output
1 2 3 4 5 


Similar Reads