Hey,
I am working on a project were I need to store a long long data type variable in six indexs of a char array. After playing with memcpy, I think it will work. However I am not understanding why if the src variable is not a pointer or array I get an error. Below are examples:
1 2 3 4 5
//This code works
int holder[2] = {17,9};
void * block = holder;
int test[5];
memcpy(&test[2], (int *)block, sizeof(test));
1 2 3 4 5
//This code doesn't works
//This is the way I had thought about for my code
int temp = 990;
unsignedshort holder[5];
memcpy(&holder[2], (unsignedshort *)temp, 4);
1 2 3 4
//This code works
int holder[1] = {1794875678};
unsignedshort des[5];
memcpy(&des[1], (unsignedshort *)holder, 4));
At this point, I would like a little more info on memcpy. I don't fully understand why my 2nd example want work.Any input on this will be greatly appreicated.
second snippet: temp is an int, NOT a pointer to int! memcpy trying to dereference "990" and causing segfault. Use
1 2 3 4 5
//This code doesn't works
//This is the way I had thought about for my code
int temp = 990;
unsignedshort holder[5];
memcpy(&holder[2], (unsignedshort *)(&temp), 4);