Skip to content
Related Articles

Related Articles

C Program to Copy All the Elements of One Array to Another Array

View Discussion
Improve Article
Save Article
  • Last Updated : 01 Aug, 2022
View Discussion
Improve Article
Save Article

To copy all the elements of one array to another in C language using three approaches. 

Example: 

Input: 

First Array:  a[5] = {3, 6, 9, 2, 5} 

Output: 

First Array : a[5] = {3, 6, 9, 2, 5}
Second Array : b[5] = {3, 6, 9, 2, 5}

Explanation: Copy elements from array a to b and then print the second array elements

Approach 1: Simple copying of elements from one array to another

C




// C program to copy all the elements
// of one array to another
#include <stdio.h>
  
int main()
{
  
    int a[5] = { 3, 6, 9, 2, 5 }, n = 5;
    int b[n], i;
  
    // copying elements from one array to another
    for (i = 0; i < n; i++) {
        b[i] = a[i];
    }
    // displaying first array before
    // copy the elements from
    // one array to other
    printf("The first array is :");
    for (i = 0; i < n; i++) {
        printf("%d ", a[i]);
    }
    // displaying array after copy
    // the elements from one
    // array to other
    printf("\nThe second array is :");
    for (i = 0; i < n; i++) {
        printf("%d ", b[i]);
    }
    return 0;
}

Output

The first array is :3 6 9 2 5 
The second array is :3 6 9 2 5 

Approach 2: Using functions

C




// C program to copy all the elements
// of one array to another using functions
#include <stdio.h>
  
int copy_array(int* a, int* b, int n)
{
    int i;
  
    // copying elements from
    // one array to another
    for (i = 0; i < n; i++) {
        b[i] = a[i];
    }
  
    // displaying array after copy
    // the elements from one
    // array to other
    for (i = 0; i < n; i++) {
        printf("%d ", b[i]);
    }
}
// displaying first array before
// copy the elements from one
// array to other
int first_array(int* a, int n)
{
    int i;
    for (i = 0; i < n; i++) {
        printf("%d ", a[i]);
    }
}
  
int main()
{
    int k[5] = { 3, 6, 9, 2, 5 }, n = 5;
    int l[n];
    printf("The first array is : ");
    first_array(k, n);
    printf("\nThe second array is : ");
    copy_array(k, l, n);
  
    return 0;
}

Output

The first array is : 3 6 9 2 5 
The second array is : 3 6 9 2 5 

Approach 3: Using Recursion

C




// C program to copy all the elements
// of one array to another using recursion
  
#include <stdio.h>
  
int copy_array(int a[], int b[], int n, int i)
{
    // copying elements from one array to another
    if (i < n) {
        b[i] = a[i];
        copy_array(a, b, n, ++i);
    }
}
int array(int a[], int n)
{
    int i;
    for (i = 0; i < n; i++) {
  
        printf("%d ", a[i]);
    }
}
  
int main()
{
    int k[5] = { 3, 6, 9, 2, 5 }, n = 5;
    int l[n], i;
  
    copy_array(k, l, n, 0);
    // displaying first array before 
    // copy the elements from
    // one array to other
  
    printf("first array : ");
    array(k, n);
  
    // displaying array after copy
    // the elements from one
    // array to other
    printf("\nsecond array : ");
    array(l, n);
    return 0;
}

Output

first array : 3 6 9 2 5 
second array : 3 6 9 2 5 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!