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

</>
Copy
#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:

  1. We declare an integer array source[] with 5 elements.
  2. We calculate the number of elements using sizeof(source) / sizeof(source[0]).
  3. We declare another integer array destination[] of the same size.
  4. We use a for loop to iterate over each index and copy values from source[i] to destination[i].
  5. 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

</>
Copy
#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:

  1. We declare an integer array source[] and determine its size.
  2. We create an integer array destination[] of the same size.
  3. We initialize two pointer variables src_ptr and dest_ptr to point to the source and destination arrays, respectively.
  4. We use a for loop to copy each element using pointer arithmetic: *(dest_ptr + i) = *(src_ptr + i).
  5. 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

</>
Copy
#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:

  1. We include the string.h library to use the memcpy() function.
  2. We declare an integer array source[] and calculate its size in bytes using sizeof(source).
  3. We create another integer array destination[] of the same size.
  4. We use memcpy(destination, source, size) to copy the contents.
  5. 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:

  1. Using a for loop – Simple and manual copying.
  2. Using pointers – Efficient way with pointer arithmetic.
  3. Using memcpy() – Built-in function for copying memory blocks.