I need to convert an array an place it in a struct that has a void* element and back to another array:
unsigned short array[size];
//do something to the array
typedef struct ck{
void * arg1;
void * arg2;
void * arg3;
} argCookie;
argCookie myCookie;
myCookie.arg2=malloc(sizeof(array));//alloc the necessary space
memcpy(myCookie.arg2,&array,sizeof(array));//copy the entire array there
//later....
unsigned short otherArray[size];
otherArray=*((unsigned short**)aCookie.arg2);
It happens that this last line won't compile... Why is that? obviously I've messed up somewhere...
Thank you.
You can't copy arrays by assigning it a pointer, arrays are not pointers, and you cannot assign to an array, you can only assign to elements of an array.
You can use memcpy() to copy into your array:
//use array, or &array[0] in memcpy,
//&array is the wrong intent (though it'll likely not matter in this case
memcpy(myCookie.arg2,array,sizeof(array));
//later....
unsigned short otherArray[size];
memcpy(otherArray, myCookie.arg2, size);
That assumes you know size
, otherwise you need to place the size in one of your cookies as well.
Depending on what you need, you might not need to copy into otherArray
, just use the data from the cookie directly:
unsigned short *tmp = aCookie.arg2;
//use `tmp` instead of otherArray.