C Program to Copy an Array to Another Array
Last Updated :
21 Nov, 2024
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;
}
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;
}
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;
}
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;
}
Similar Reads
C Program to Traverse an Array Write a C program to traverse the given array that contains N number of elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3} Output: 2 -1 5 6 0 -3Input: arr[] = {4, 0, -2, -9, -7, 1} Output: 4 0 -2 -9 -7 1Different Ways to Traverse an Array in CArrays are versatile data structures and C language pro
3 min read
C Program To Merge Two Arrays Merging two arrays means combining/concatenating the elements of both arrays into a single array.ExampleInput: arr1 = [1, 3, 5], arr2 = [2, 4, 6]Output: res = [1, 3, 5, 2, 4, 6]Explanation: The elements from both arrays are merged into a single array.Input: arr1 = [10, 40, 30], arr2 = [15, 25, 5]Out
3 min read
C Program to Sort an Array in Ascending Order Sorting an array in ascending order means arranging the elements in the order from smallest element to largest element.The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array. Let's look at a simple example:C
3 min read
How to Convert a String to a Char Array in C? In C, the only difference between the string and a character array is possibly the null character '\0' but strings can also be declared as character pointer in which case, its characters are immutable. In this article, we will learn how to convert a string to a char array in C.The most straightforwa
2 min read
Program to copy the contents of one array into another in the reverse order Given an array, the task is to copy these array elements into another array in reverse array.Examples: Input: array: 1 2 3 4 5 Output: 5 4 3 2 1 Input: array: 10 20 30 40 50 Output: 50 40 30 20 10 Let len be the length of original array. We copy every element original_arr[i] to copy_arr[n-i-1] to ge
7 min read