Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying data in pointers

Tags:

c

pointers

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.

like image 246
some_id Avatar asked May 12 '11 10:05

some_id


1 Answers

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));
}
like image 72
Mihran Hovsepyan Avatar answered Jan 03 '23 11:01

Mihran Hovsepyan