📊Beginner

Copy All Elements to Another Array

Array copy

Copying an array involves creating a new array and copying each element from the source to the destination array using a loop.

C Program to Copy Array Elements

copy_array.c
C
1#include <stdio.h>
2
3int main() {
4 int source[] = {10, 20, 30, 40, 50};
5 int n = sizeof(source) / sizeof(source[0]);
6 int dest[n]; // Destination array
7
8 // Copy elements
9 for (int i = 0; i < n; i++) {
10 dest[i] = source[i];
11 }
12
13 // Print both arrays
14 printf("Source array: ");
15 for (int i = 0; i < n; i++) {
16 printf("%d ", source[i]);
17 }
18
19 printf("\nDestination array: ");
20 for (int i = 0; i < n; i++) {
21 printf("%d ", dest[i]);
22 }
23
24 return 0;
25}
Output

Source array: 10 20 30 40 50

Destination array: 10 20 30 40 50

🎯 Key Takeaways

Cannot assign arrays directly (arr1 = arr2)
Must copy element by element