The moment you do bb = first;, bb and first are pointing to the same location of memory. first->a = 55; first->b = 55; first->c = 89; will change the values for a, b, and c in that location. The original value of first, is still lingering in memory but no way to access it anymore.
I think what you may want to do is *bb = *first;.
The moment you do bb = first;, bb and first are pointing to the same location of memory. first->a = 55; first->b = 55; first->c = 89; will change the values for a, b, and c in that location. The original value of first, is still lingering in memory but no way to access it anymore.
I think what you may want to do is *bb = *first;.
Your knowledge about memcpy is correct but you cannot assign contents of "location pointed to by the pointers" just by assigning pointers like you did in your statement mentioned above.
You are assigning one pointer to the another in the following statement:
bb = first;
Now both these point to the same memory location (think of bb as an alias to first).
If you want to copy data then you copy using "data pointed to by pointers" *bb = *first
Copying pointers. - C++ Forum
c - When should I copy a pointer inside a function? - Software Engineering Stack Exchange
Copy pointer to pointer in function - C - C++ Forum
c - Copying data in pointers - Stack Overflow
Videos
From a technical point of view, using an additional variable ptr to make it possible to return the original destination value is unneccessary, since the caller obviously must already have the value of destination in his scope. If you design a strcpy-like function without returning that value, you can create a more comprehensive implementation, as you have shown above. But the original strcpy function from the standard library is defined to return destination, and my_strcpy obviuosly tries to mimic that behaviour/signature.
The reason for this is mainly "syntactic sugar", allowing the use of strcpy in a call chain. Read this SO post to get a better explanation for what it is good for.
To better understand the pointers I find it useful to put asterisk near the type, like char* destination. This way you see that the value you pass has type "pointer to char" and you simply think of it as an integer (usually 32-bit or 64-bit) that in itself you may modify without any consequences. Your understanding is probably already good of this, because this is the reason a local copy is unnecessary. The case when with strcpy it is useful is to return the original value as noted by the other answer.
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));
}
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;
}
C is a pass-by-value language. This question of the C FAQ can answer your question. Note that even if you fix your function prototype, your algorithm isn't copying a string at all - just a pointer. You can just replace that call with:
d = s;
In your main function. The malloc is unnecessary, too - you're just leaking it.
It is because c language is pass by value. Instead pass the address of the pointer.
void my_strcpy(char *sour,char **dest)
// Condition checking and then do -
*dest = sour ;