Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Copying one array to another array

To copy the whole array into another array, we should copy each element of one array to another array, separately.
It is not possible to copy all the elements of one array to another array by just assigning a name of one array to another array.

Example

#include <stdio.h>
int main()
{
    int x[10] = {10,20,30,40,50,60,70,80,90,100};
    int y[10];
    x = y;     // Error
	
    return 0;
}

We must copy each element of one array to another array, separately as shown below:

Example

#include <stdio.h>
int main()
{
    int x[10] = {10,20,30,40,50,60,70,80,90,100};
    int y[10];
    int i;
	
    for(i=0;i<10;i++)
    {
        y[i] = x[i];  // copying each element one by one
    }
	
    printf("\n Elements of array y[10] :: ");
    for(i=0;i<10;i++)
    {
        printf("\n %d",y[i]);
    }
    return 0;
}

Output

Elements of array y[10] ::
10
20
30
40
50
60
70
80
90
100

 
 

Leave a reply