Swapping values using *pointers

I'm trying to swap to values using pointers to point to the address of my variables. Please note that I know you have to use &theseed when seeding the values.

however why doesn't my function work? Why is itemp refusing to store the value of ix? (I thought after I had stored my variable to the pointer, I would be working exclusively with the stored variable)

1
2
3
4
5
6
7
8
void Swap_real(int* ix, int* ix2)
{
	int itemp;
	itemp = ix;
	ix = ix2;
	ix2 = itemp;

}
because itemp is an int and not an int*

EDIT: I think you could also use std::swap instead of your own function, I assume it works with pointers...

EDIT 2: You're also passing by value, so it will have no effect on the original pointers, pass a reference to a pointer
Last edited on
I thought I was working with the value I seeded into the pointer
Seeded into the pointer? With the possible exception of dynamically allocated memory pointers don't hold data, they just point to the variable you told it to. All your function would do when you get it working is make ix point to what ever ix2 was pointing at and vica versa. It would be much simpler to just change what each one is pointed at. Unless, like I said above, they are dynamically allocated.
Last edited on
thanks guys, I've learnt that I had forgotten to use the * when telling it to work with the variable being pointed to.
Topic archived. No new replies allowed.