Reputation: 29896
How does one copy the data that is pointed to by another pointer?
I have the following
void *startgpswatchdog(void *ptr)
{
GPSLocation *destination;
*destination = (GPSLocation *) ptr;
Will this do this correctly?
I free the data that is passed into thread after passing it, so I need to copy the data.
Upvotes: 6
Views: 26312
Reputation: 13315
you need to allocate memory before you assign to the address pointed by the pointer. why do you need a pointer here ? why not use
void *startgpswatchdog(void *ptr)
{
GPSLocation destination;
destination = (GPSLocation) *ptr;
}
and later if you need this variable address just use
&destination
just dont forget its a local variable :)
Upvotes: 0
Reputation:
You can do it if the pointer you ae copying to actually points at something:
void *startgpswatchdog(void *ptr)
{
GPSLocation *destination = malloc( sizeof( GPSLocation ) );
*destination = * (GPSLocation *) ptr;
}
or perhaps better:
void *startgpswatchdog(void *ptr)
{
GPSLocation destination;
destination = * (GPSLocation *) ptr;
}
Upvotes: 0
Reputation: 11108
If you want to copy data you should allocate new memory via malloc
, then copy your memory via memcpy
.
void *startgpswatchdog(void *ptr)
{
GPSLocation *destination = malloc(sizeof(GPSLocation));
memcpy(destination, ptr, sizeof(GPSLocation));
}
Upvotes: 12