Archived

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

TheRealBartman

Pointer Question

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
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
That code i posted does not change what the pointers point to. For example, this snipit

    
#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);
s1->x = 1;
s1->y = 3;
printf("After2: %d %d %d %d\n", s1->x, s1->y, s2->x, s2->y);
}

would output

...
After2: 1 3 5 10


[edited by - kdogg on May 6, 2003 1:19:53 PM]

Share this post


Link to post
Share on other sites
something *s1, *s2, s3;

s1 = new something;
s2 = new something;
s3 = new something;

// copy pointer
s2 = s3;

// copy contents
*s1 = *s2

delete s2;
delete s3;

/*
result: s1''s contents are now equal to what s3''s contents were before it was deleted.
*/

Share this post


Link to post
Share on other sites