I am having trouble giving a struct value to a C interface for a queue that manages the queue as a linked list. It holds onto the data as void pointers to allow the interface to manage any data generically. I pass in the value as a pointer reference which is then saved as a void pointer. Later when it is returned I do not know how to cast the void pointer back to the original value.
What do I need to do? Here is the sample struct.
typedef struct MyData {
int mNumber;
} MyData;
To simplify everything I have created a dummy function that simulates everything in a few lines of code.
void * give_and_go(void *data) {
void *tmp = data;
return tmp;
}
You can see it takes in a void pointer, sets it as a local variable and then returns it. Now I need to get the original value back.
MyData inVal;
inVal.mNumber = 100;
void * ptr = give_and_go(&inVal);
MyData outVal; // What converts ptr to the out value?
This is where I am stuck. I am using Xcode and it will not allow me to simply cast ptr to MyData or various alternatives that I have tried.
Any help is appreciated.
Cast your void* back to a MyData*, then dereference it.
MyData *outVal = (MyData*)(ptr);