Copy an Array in C

In the C programming language, we can copy an array using loops. For this first, we need to create an array of similar data types and sizes then perform the copy operation.

Example:-
Original array = {10, 20, 30, 40, 50};
Copied array = {10, 20, 30, 40, 50};

To create a copy of the array in C using loop,
1) Create a new array with a similar data type and size.
2) Use a loop to iterate through the original array.
3) Copy the ith element of the original array to the ith element of the new array.

Before creating a new array, first, calculate the size of the original array. The sizeof() function can be used to calculate the size of an array in C. Now, let us demonstrate the copy of the array using loops.

#include <stdio.h>
// function to copy an array
void copyArray(int arr[], int copy[], int size)
{
  // loop to iterate through array
  for (int i = 0; i < size; ++i)
  {
    copy[i] = arr[i];
  }
}
// function to display an array
void displayArray(int arr[], int size)
{
  for (int i = 0; i < size; i++) {
    printf("%d ",arr[i]);
  }
}
// main function
int main()
{
  // original array
  int arr[] = {10, 20, 30, 40, 50};

  // calculate size
  int size = sizeof(arr)/sizeof(arr[0]);
      
  // create new array
  int newArr[size];

  // copy array
  copyArray(arr, newArr, size);
      
  // display array
  printf("Original array = ");
  displayArray(arr, size);
  printf("\nCopied array = ");
  displayArray(newArr, size);

  return 0;
}

Output:-

Original array = 10 20 30 40 50
Copied array = 10 20 30 40 50

In the main method, we have taken an array with existing values. You can read input from the end-user. Later using the sizeof() function we calculated the size of the array. Now, create a new array of similar sizes and data types. After that, we had called the copyArray() method.

The copyArray() method of the above program takes both arrays and the size. Now, it is using the for loop to copy each element of the original array to the new array. 

Finally, the displayArray() method is called which is used to display the given array. It is accepting the array and its size.

Note: Here each element of the original array is assigned to new array elements therefore whenever we change the content of the original array then-new array elements won’t be affected. 

#include <stdio.h>
// main function
int main()
{
  // original array
  int arr[] = {10, 20, 30, 40, 50};

  // calculate size
  int size = sizeof(arr)/sizeof(arr[0]);
      
  // create new array
  int newArr[size];

  // copy array
  copyArray(arr, newArr, size);

  // display array (Before Modification)
  printf("Before Modification,");
  // display array
  printf("\nOriginal array = ");
  displayArray(arr, size);
  printf("\nCopied array = ");
  displayArray(newArr, size);

  // modify original array content
  arr[0] = 99;
  arr[1] = 88;

  // display array (After Modification)
  printf("\nAfter Modification,");
  printf("\nOriginal array = ");
  displayArray(arr, size);
  printf("\nCopied array = ");
  displayArray(newArr, size);

  return 0;
}

Output:-

Before Modification,
Original array = 10 20 30 40 50
Copied array = 10 20 30 40 50
After Modification,
Original array = 99 88 30 40 50
Copied array = 10 20 30 40 50

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *