How to Copy One Array to Another in C
To copy one array to another in C, we can use loops to assign each element from the source array to the destination array.
The simplest way to do this is by using a for
loop, iterating over each index and copying the corresponding value. Other methods include using pointers and the memcpy()
function from the C standard library.
In this tutorial, we will go through the examples covering each of these approaches to copy one array to another.
Examples of Copying an Array
1. Copying an Array Using a for
Loop
In this example, we will copy an integer array to another using a for
loop. We will iterate over the source array and assign each element to the destination array.
main.c
#include <stdio.h>
int main() {
int source[] = {1, 2, 3, 4, 5};
int size = sizeof(source) / sizeof(source[0]);
int destination[size];
// Copying elements using for loop
for (int i = 0; i < size; i++) {
destination[i] = source[i];
}
// Printing the copied array
printf("Copied Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", destination[i]);
}
return 0;
}
Explanation:
- We declare an integer array
source[]
with 5 elements. - We calculate the number of elements using
sizeof(source) / sizeof(source[0])
. - We declare another integer array
destination[]
of the same size. - We use a
for
loop to iterate over each index and copy values fromsource[i]
todestination[i]
. - We use another
for
loop to print the copied array.
Output:
Copied Array: 1 2 3 4 5
2. Copying an Array Using Pointers
In this example, we will use pointers to copy elements from one array to another.
main.c
#include <stdio.h>
int main() {
int source[] = {10, 20, 30, 40, 50};
int size = sizeof(source) / sizeof(source[0]);
int destination[size];
int *src_ptr = source;
int *dest_ptr = destination;
// Copying elements using pointers
for (int i = 0; i < size; i++) {
*(dest_ptr + i) = *(src_ptr + i);
}
// Printing the copied array
printf("Copied Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", destination[i]);
}
return 0;
}
Explanation:
- We declare an integer array
source[]
and determine its size. - We create an integer array
destination[]
of the same size. - We initialize two pointer variables
src_ptr
anddest_ptr
to point to the source and destination arrays, respectively. - We use a
for
loop to copy each element using pointer arithmetic:*(dest_ptr + i) = *(src_ptr + i)
. - We print the copied array using a loop.
Output:
Copied Array: 10 20 30 40 50
3. Copying an Array Using memcpy()
In this example, we will use the memcpy()
function from the string.h
library to copy an array.
main.c
#include <stdio.h>
#include <string.h>
int main() {
int source[] = {100, 200, 300, 400, 500};
int size = sizeof(source);
int destination[5];
// Copying array using memcpy
memcpy(destination, source, size);
// Printing the copied array
printf("Copied Array: ");
for (int i = 0; i < size / sizeof(int); i++) {
printf("%d ", destination[i]);
}
return 0;
}
Explanation:
- We include the
string.h
library to use thememcpy()
function. - We declare an integer array
source[]
and calculate its size in bytes usingsizeof(source)
. - We create another integer array
destination[]
of the same size. - We use
memcpy(destination, source, size)
to copy the contents. - We print the copied array using a
for
loop.
Output:
Copied Array: 100 200 300 400 500
Conclusion
We explored different methods to copy one array to another in C:
- Using a
for
loop – Simple and manual copying. - Using pointers – Efficient way with pointer arithmetic.
- Using
memcpy()
– Built-in function for copying memory blocks.