Copy data in pointers

How to copy data pointed to by another pointer?

I have the following

void *startgpswatchdog(void *ptr)
{
    GPSLocation *destination;
    *destination = (GPSLocation *) ptr;

Will it do it right?

I release the data that is transferred to the stream after it is transferred, so I need to copy the data.

+3
source share
3 answers

If you want to copy data, you must allocate a new memory through malloc, and then copy your memory through memcpy.

void *startgpswatchdog(void *ptr)
{
    GPSLocation *destination = malloc(sizeof(GPSLocation));
    memcpy(destination, ptr, sizeof(GPSLocation));
}
+8
source

You can do this if the pointer you are copying is actually pointing to 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;
}
0
source

, , . ?

void *startgpswatchdog(void *ptr)
{
GPSLocation destination;
destination = (GPSLocation) *ptr;
}

, ,

&destination

:)

0

All Articles