#include <stdio.h>
int main(){
int a[4];
int b[4],i;
a[0] = 4;
a[1] = 3;
a[2] = 2;
a[3] = 1;
memcpy(&b, &a, sizeof(a));
for (i = 0; i < 4; i++){
printf("b[%d]:%d",i,b[i]);
}
printf("%d",sizeof(b));
}
ANS:
b[0]:4b[1]:3b[2]:2b[3]:116
Exited: ExitFailure 2
I'm getting the correct answers. But getting a exception as Exited: ExitFailure 2.
Is this way of copying the array datas using memcpy is wrong?
return 0;at the end.memcpykind of wrong here, you don't need the address-of operator (&) as array-of-type is compatible with pointer-of-type, somemcpy(b, a, sizeof(a));is enough.&is optional for arrays. I always leave it out. But for pointers, it's obviously different.