I have an integer array:
int num[20] = {1,1,5,5,1,1,5,9,2,2,6,1,1,2,5,5,1,3,6,2};
I want to copy the elements num into the following struct:
struct tuple
{
int p1;
int p2;
int p3;
int p4;
};
I am doing the following:
struct tuple *arr;
memcpy(&arr,&num,sizeof(num));
This does not seem to work, since I am encountering a segmentation fault later on in the code. When I try to print the size:
printf("size of arr: %lu, size of arr[0]: %lu \n", sizeof(arr), sizeof(arr[0]));
I get the following:
size of arr: 8, size of arr[0]: 16
which is wrong, since the values should read:
size of arr: 80, size of arr[0]: 16
Therefore when I try to print, it seg faults:
for (i=0;i<sizeof(arr)/sizeof(arr[0]);++i)
printf("%d,%d,%d,%d\n", arr[i].p1,arr[i].p2, arr[i].p3, arr[i].p4);
Can someone assist me as to where I might be going wrong?
memcpyto an uninitialized pointer.arris a pointer, that that is eight bytes large is not surprising.memcpyis the address of the pointer. Not sure whether that's worse or better than an uninitialised pointer.memcpy(arr,num,sizeof(num));vs.memcpy(&arr,&num,sizeof(num));But the are many other problems than this. Need new approach.