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; } Output1 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:Table of ContentUsing a LoopUsing RecursionUsing PointersUsing a LoopThis 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; } Output1 2 3 4 5 This method provides more control of the copying process.Using RecursionIn 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; } Output1 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 PointersThis 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; } Output1 2 3 4 5 Comment More infoAdvertise with us Next Article C Program to Copy an Array to Another Array L laxmigangarajula03 Follow Improve Article Tags : C Programs C Language C Array Programs 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 Copy N Characters from One String to Another Without strncat() In C, strings are the sequence of characters that are used to represent the textual data. Two strings can be easily concatenated or joined using the strncat() function of the C standard library up to n characters. In this article, we will learn how to concatenate the first n characters from one stri 2 min read How to Add an Element to an Array of Structs in C? In C, a struct is a user-defined data type that allows the users to group related data in a single object. An array of structs allows to store multiple structs in contiguous memory locations. In this article, we will learn how to add an element to an array of structs in C. Example: Input: structArra 3 min read How to Create a Dynamic Array of Structs? In C, we have dynamic arrays in which we can allocate an array of elements whose size is determined during runtime. In this article, we will learn how to create a dynamic array of structures in C. Create a Dynamic Array of Structs in CA dynamic array of structs in C combines dynamic arrays and struc 2 min read C Program to Implement Queue using Array A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle which means the elements added first in a queue will be removed first from the queue. In this article, we will learn how to implement a Queue using Array in C. Implement Queue using Array in CTo implement Queue u 6 min read How to Create an Array of Structs in C? In C, a structure is a user-defined data type that can be used to group items of different types into a single entity while an array is a collection of similar data elements. In this article, we will learn how to create an array of structs in C. Creating an Array of Structs in CTo create an array of 2 min read Like