c language array copy

Two methods can be achieved.

For the convenience of explanation, define two integer arrays a and b, and realize the assignment of the value in a to b.
int a[4] = {1,2,3,4}, b[4];

1. Traverse the array and assign values ​​one by one.
Define the loop variable int i;
for(i = 0; i <4; i ++)
b[i] = a[i];
The function of this program is to traverse the array a and assign values ​​to the corresponding elements of the array b one by one.

2. Use the memory copy function memcpy to assign values ​​as a whole.
void *memcpy(void *dst, void *src, size_t size);
The function of this function is to assign size bytes of data from src to dst.
When calling this function, you need to quote the header file cstring, that is , the code of
#include
assignment array is
memcpy(b,a,sizeof(a));

Guess you like

Origin blog.csdn.net/m0_53052839/article/details/111872935