Pointer Question

Started by
10 comments, last by TheRealBartman 20 years, 11 months ago
How would you copy the contents of one pointer into another, without manually typing out everything to be copied? Like say I had a CharNode pointer called TempChar, and I want to copy it into another CharNode pointer called CurChar. Basically, what I am asking is how would I copy the contents of what Temp is pointing at to the contents of what CurChar is pointing at? Every way that I have tried so far ends up making CurChar point to TempChar, and when TempChar is deleted then CurChar is messed up. Thanks, Michael Bartman
Michael BartmanLead ProgrammerDark Omen Studios
Advertisement
Copy constructors?


[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people
[size=2]
Thanks for the quick reply!

That''s probably something incredibly simple, but I don''t know what it is by that name.

Michael Bartman
Michael BartmanLead ProgrammerDark Omen Studios
Uh...a for loop or memcpy would probably do the trick for copying contents of arrays over, if that''s what you''re referring to. Can you perhaps give an example?

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

Hey,

It''s not an array, it is a pointer to a struct.

I tried memcpy, and it ended up copying the address of the pointer instead of the contents.

That was with a different compiler than I have now, so I''ll give it a wiz again.
Michael BartmanLead ProgrammerDark Omen Studios

  class A{        int x;    public:         A(int y = 0) { x = y; }         // Copy constructor:         A(const A& a) { x = a.x; }};// ...A* firstptr = new A;A* secondptr = new A(*firstptr);  
Alright, thanks.

That''s what I was trying to avoid doing, but I guess it is the only correct way.

Thanks again
Michael BartmanLead ProgrammerDark Omen Studios
If it''s a plain old data type, you can just use the ''*'' operator. For example:


  #include <stdio.h>typedef struct{  int x;  int y;} mystruct;main(){  mystruct *s1, *s2;  s1 = (mystruct*)malloc(sizeof(mystruct));  s2 = (mystruct*)malloc(sizeof(mystruct));  s1->x = 5;  s1->y = 10;  s2->x = 7;  s2->y = 12;  printf("Before: %d %d %d %d\n", s1->x, s1->y, s2->x, s2->y);  *s2 = *s1;  printf("After: %d %d %d %d\n", s1->x, s1->y, s2->x, s2->y);}  


will output

Before: 5 10 7 12
After: 5 10 5 10

If it''s a class or something, that should still work if you overload the ''='' operator.
But my problem is that the pointer then POINTs to the other pointer, instead of copying all of the contents of the pointer.

Then when you delete s1, s2 will be garbage.

At least that is what is happening to me.....

Thanks
Michael BartmanLead ProgrammerDark Omen Studios
if your pointer address is changing, then there''s something syntactically wrong with your code. as long as you''re de-referencing the pointer, the address that it points to shouldn''t change, barring a compiler error.

as kdogg said, use the ''*'' operator: *pointer1 = *pointer2;
or use brackets: pointer1[0] = pointer2[0];

also, memcpy works fine. it expects 2 pointers, so it will de-reference the first 2 parameters, whether or not they are pointers. if it copied the address, you must have passed in the address of your pointer.

This topic is closed to new replies.

Advertisement