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
Copy pointer to pointer in function - C - C++ Forum
c - When should I copy a pointer inside a function? - Software Engineering Stack Exchange
c - Copying data in pointers - Stack Overflow
c - How to copy data to a pointer returned by malloc? - 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;
}
I think you have a misunderstanding of what is going on. The statement *p1 = num2; is not changing the value of p1, it is changing the value of whatever p1 points to. Since both p1 and p2 point to the same variable when you change its value through one pointer you will see the same value when you dereference either pointer. What you want to do is have p1 point to num2, not take the value of num2 and store it in whatever p1 points to.
Change
*p1 = num2;
to
p1 = &num2;
Consider the following code:
int a = 5, b = 10, *foo, *bar;
foo = &a; // foo now points at the address of A
bar = foo; // bar now points at the address at which foo is pointing
foo = &b; // foo now points at the address of B
cout << *foo; // prints 10
cout << *bar; // prints 5
Thats the behaviour you want to achieve if Im not mistaken.
Your code is nonsensical however. IF I'm not wrong, your mistake is that you create a second pointer which points at the address of the first pointer instead of pointing at the address from the first pointer.
To copy strings in C, you can use strcpy. Here is an example:
#include <stdio.h>
#include <string.h>
const char * my_str = "Content";
char * my_copy;
my_copy = malloc(sizeof(char) * (strlen(my_str) + 1));
strcpy(my_copy,my_str);
If you want to avoid accidental buffer overflows, use strncpy instead of strcpy. For example:
const char * my_str = "Content";
const size_t len_my_str = strlen(my_str) + 1;
char * my_copy = malloc(len_my_str);
strncpy(my_copy, my_str, len_my_str);
To perform such manual copy:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char* orig_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* ptr = orig_str;
// Memory layout for orig_str:
// ------------------------------------------------------------------------
// |0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26| --> indices
// ------------------------------------------------------------------------
// |A|B|C|D|E|F|G|H|I|J|K |L |M |N |O |P |Q |R |S |T |U |V |W |X |Y |Z |\0| --> data
// ------------------------------------------------------------------------
int orig_str_size = 0;
char* bkup_copy = NULL;
// Count the number of characters in the original string
while (*ptr++ != '\0')
orig_str_size++;
printf("Size of the original string: %d\n", orig_str_size);
/* Dynamically allocate space for the backup copy */
// Why orig_str_size plus 1? We add +1 to account for the mandatory
// '\0' at the end of the string.
bkup_copy = (char*) malloc((orig_str_size+1) * sizeof(char));
// Place the '\0' character at the end of the backup string.
bkup_copy[orig_str_size] = '\0';
// Current memory layout for bkup_copy:
// ------------------------------------------------------------------------
// |0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26| --> indices
// ------------------------------------------------------------------------
// | | | | | | | | | | | | | | | | | | | | | | | | | | |\0| --> data
// ------------------------------------------------------------------------
/* Finally, copy the characters from one string to the other */
// Remember to reset the helper pointer so it points to the beginning
// of the original string!
ptr = &orig_str[0];
int idx = 0;
while (*ptr != '\0')
bkup_copy[idx++] = *ptr++;
printf("Original String: %s\n", orig_str);
printf("Backup String: %s\n", bkup_copy);
return 0;
}