Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

TheRealBartman

Pointer Question

This topic is 5555 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

  
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);

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!