- How to copy elements from one array to another
- Two methods: using a loop and using memcpy
- How to display both source and destination arrays
- Step-by-step explanation of the program
- Practice exercises to test your understanding
Introduction
In this tutorial, we will learn how to write a C program to copy elements from one array to another.
Copying an array is a common operation in programming. It is used in many real-world applications, such as:
- Creating a backup copy of data before modification
- Passing data to functions without modifying the original
- Sorting or processing data while keeping the original intact
- Duplicating data for parallel processing
💡 Key Point: When copying an array, you need to copy each element individually. Unlike some languages, C does not allow direct array assignment with the = operator.
C Program to Copy Array Elements (Method 1: Using Loop)
#include <stdio.h>
int main() {
int n, i;
// Ask user for number of elements
printf("Enter the number of elements: ");
scanf("%d", &n);
// Declare arrays
int source[n];
int destination[n];
// Read elements into the source array
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &source[i]);
}
// Copy elements from source to destination
for(i = 0; i < n; i++) {
destination[i] = source[i];
}
// Display source array
printf("\nSource array: ");
for(i = 0; i < n; i++) {
printf("%d ", source[i]);
}
// Display destination array
printf("\nDestination array: ");
for(i = 0; i < n; i++) {
printf("%d ", destination[i]);
}
printf("\n");
return 0;
}
Sample Output
Enter the number of elements: 5 Enter 5 elements: 10 20 30 40 50 Source array: 10 20 30 40 50 Destination array: 10 20 30 40 50
Another Example:
Enter the number of elements: 4 Enter 4 elements: 5 15 25 35 Source array: 5 15 25 35 Destination array: 5 15 25 35
Program Explanation
Let's break down the code step by step:
- Include Header File:
#include <stdio.h>includes the standard input/output library. - Declare Variables:
int n;— stores the number of elementsint i;— loop counter
- Declare Arrays:
int source[n];— the original arrayint destination[n];— the array where elements will be copied
- Get User Input: Prompts the user to enter the number of elements and then reads them into the source array.
- Copy the Array: The
forloop copies each element from source to destination:destination[i] = source[i];- This copies the value at index i from source to index i in destination
- Display Results: Prints both the source and destination arrays to verify the copy.
- Return:
return 0;indicates successful program execution.
📝 Note: In C, you cannot assign one array to another directly using destination = source;. You must copy each element individually using a loop.
Method 2: Using memcpy Function (More Efficient)
The memcpy() function from string.h library provides a faster way to copy arrays. It copies a block of memory from source to destination.
#include <stdio.h>
#include <string.h> // Required for memcpy
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int source[n];
int destination[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &source[i]);
}
// Copy using memcpy
memcpy(destination, source, n * sizeof(int));
// Display source array
printf("\nSource array: ");
for(i = 0; i < n; i++) {
printf("%d ", source[i]);
}
// Display destination array
printf("\nDestination array: ");
for(i = 0; i < n; i++) {
printf("%d ", destination[i]);
}
printf("\n");
return 0;
}
Sample Output:
Enter the number of elements: 6 Enter 6 elements: 10 20 30 40 50 60 Source array: 10 20 30 40 50 60 Destination array: 10 20 30 40 50 60
How memcpy Works
memcpy(destination, source, n * sizeof(int));- destination: Pointer to the destination array
- source: Pointer to the source array
- n * sizeof(int): Number of bytes to copy
- Advantage: Faster than manual loop for large arrays
Algorithm to Copy Array Elements
- Start
- Read the number of elements (n)
- Read n elements into the source array
- For
i = 0ton-1:- Set
destination[i] = source[i]
- Set
- Print both source and destination arrays
- End
💻 Practice Exercise
Challenge 1: Write a program to copy only the even numbers from one array to another.
Challenge 2: Copy an array in reverse order to another array.
🔍 Click to Show Solution for Challenge 2
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int source[n];
int destination[n];
printf("Enter %d elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &source[i]);
}
// Copy in reverse order
for(i = 0; i < n; i++) {
destination[i] = source[n - 1 - i];
}
printf("\nSource array: ");
for(i = 0; i < n; i++) {
printf("%d ", source[i]);
}
printf("\nDestination (reversed): ");
for(i = 0; i < n; i++) {
printf("%d ", destination[i]);
}
printf("\n");
return 0;
}
Frequently Asked Questions
1. How do you copy an array in C?
You can copy an array in C using a loop (copying each element individually) or using the memcpy function from the string.h library.
2. Can I assign one array to another using the equals operator?
No, you cannot use destination = source; in C. You must copy each element individually using a loop or use memcpy().
3. Which method is faster: loop or memcpy?
The memcpy function is generally faster because it is optimized in C libraries. For large arrays, memcpy is significantly faster than a manual loop.
4. What if the arrays have different sizes?
Both arrays should have the same size. If the destination array is larger, only the first n elements will be copied. If it's smaller, you'll get a buffer overflow error.
5. Can I copy arrays of different data types?
No, arrays of different data types cannot be directly copied. You would need to convert each element individually or use typecasting.
💡 Tip: When using memcpy, always ensure the destination array is large enough to hold the copied data to avoid buffer overflow.