I have a callback function which has the parameter const unsigned char *pData. everytime I hit the callback function I need to store the pData value into my local unsigned char* variable. Is there any function to copy the data?
Edit: Here is a code sample:
void Callbackfun(int x, const unsigned char* pData, const UINT cbData) {
switch(x) {
case 0:
// ptr is a global variable of structure containg name and number
ptr.name = (unsigned char*)pData;
break;
case 1:
ptr.number = (unsigned char*)pData;
break;
}
}
now every time this function is called i want to store the pData values in my local structure (as shown by ptr.name).
In your callback function, you will have to allocate local memory for your “data”. That way you can retain it when the calling function leaves scope. If you know the length of the data, and the length is consistent you have two options. Dynamic allocation, or allocate on the stack. Code example is untested.
Here’s the dynamic allocation and copy.
Here’s the stack allocated version
I’m assuming you’re not passing string data since you’re using unsigned char. Since you’re only dealing with a pointer you’ll have to know the size of the buffer for either allocation.
If you do not have a constant data length, then you’ll need to pass that as a second parameter to your callback function.