Pointer Reassignment


Kagey

Still Fresh
Joined
Dec 4, 2006
Messages
32
This is my first foray into game programming, I'm confused and this has been bugging me for over a week now.

I've managed to get a breakout style clone working, with one level and fixed block positions. I'm trying to work out how to do multiple levels with different numbers/placements of bricks and to my mind, I'll need to play with pointers and dynamically allocating memory.

This example is just to try and prove repointing, then I'll deal with the array side of things for multiple bricks

To prove a point, I've tried creating a pointer and passing it to a function, which "to my understanding" repoints
the pointer and effectively changes its value. The new address/value seems to change within the function but
returns back to the original value in main() as though the new value goes out of scope after the function finishes.

I've read many examples, but they all focus on repointing pointers within a main() function.

Thanks in advance for any help offered

The printf statements give me this:
Address Value Value Address
Before changing value 134520840 20 134520840
Address *topointto 134520856 5 134520856
Before in function 134520840 20 134520840
After in function 134520856 5 134520856
After changing value 134520840 20 134520840



void repoint_existing_pointer(int *intp)
{
int *topointto = new int;
*topointto = 5;

printf("Address *topointto %15i %15i %15i\n", topointto, *topointto, &*topointto);

printf("Before in function %15i %15i %15i\n", intp, *intp, &*intp);
intp = topointto; // try and repoint the pointer to the address containing 5
printf("After in function %15i %15i %15i\n", intp, *intp, &*intp);
}


int main()
{
printf(" %15s %15s %15s\n", "Address", "Value", "Value Address");
int *intpointer = new int;
*intpointer = 20;
printf("Before changing value %15i %15i %15i\n", intpointer, *intpointer, &*intpointer);
repoint_existing_pointer(intpointer);
printf("After changing value %15i %15i %15i\n", intpointer, *intpointer, &*intpointer);

return 0;
}
 
Kagey said:
void repoint_existing_pointer(int *intp)
{
int *topointto = new int;
*topointto = 5;

printf("Address *topointto %15i %15i %15i\n", topointto, *topointto, &*topointto);

printf("Before in function %15i %15i %15i\n", intp, *intp, &*intp);
intp = topointto; // try and repoint the pointer to the address containing 5
printf("After in function %15i %15i %15i\n", intp, *intp, &*intp);
}

int main()
{
printf(" %15s %15s %15s\n", "Address", "Value", "Value Address");
int *intpointer = new int;
*intpointer = 20;
printf("Before changing value %15i %15i %15i\n", intpointer, *intpointer, &*intpointer);
repoint_existing_pointer(intpointer);
printf("After changing value %15i %15i %15i\n", intpointer, *intpointer, &*intpointer);

return 0;
}
You are passing in the value of the pointer, but you can't change the pointer knowing only its value. You could just as easily pass in a pointer that doesn't have any address:
CODE
int i = 4;
repoint_existing_pointer(&i);


What you need to do is pass in the address of the pointer so the function knows where it needs to go and store the new pointer value. So you need either a pointer or a reference (C++) to your pointer:
CODE
void reference_to_pointer(int *&intp)
{
// what you had before
}

void pointer_to_pointer(int **intp)
{
// ...
*intp = topointto;
// ...
}

int main()
{
// ...
reference_to_pointer(intpointer);
pointer_to_pointer(&intpointer);
// ...
}
 
Last edited by a moderator:
Thank you so much. :D

That combination of symbols "*&intp" hadn't even crossed my mind. :rolleyes:
 
MAy I ask why the pointer to pointer function is **intp and not *intp?

Edit; just ot be sure & passes the adress, right? and *passes the actual value of a pointer? I'm not clear about pointers :S
 
Spot on Sam!

Having the function as *intp will give you a pointer to the value, which you can change freely. However, if you want to modify the /pointer/ to that value, then you need to pass in the address of that pointer, so you need & and then since you are passing a pointer to a pointer to a value, the function is therefore **intp. If you wanted a pointer to a pointer to a pointer to a value, it would be ***intp, and so on.
 
Squidge said:
Spot on Sam!

Having the function as *intp will give you a pointer to the value, which you can change freely. However, if you want to modify the /pointer/ to that value, then you need to pass in the address of that pointer, so you need & and then since you are passing a pointer to a pointer to a value, the function is therefore **intp. If you wanted a pointer to a pointer to a pointer to a value, it would be ***intp, and so on.
Might be better/simpler to understand if you just return a newed pointer.

Don't forget to delete when appropriate as well (you are creating a memory leak at the moment).
 
Last edited by a moderator:
Back
Top