Quantcast
Channel: C++ Swapping Pointers - Stack Overflow
Browsing all 8 articles
Browse latest View live

Answer by LeeProgrammer for C++ Swapping Pointers

void swapPointer(int* &ptr1, int* &ptr2) { int* temp = ptr2; ptr2 = ptr1; ptr1 = temp;}It can be resolve by using reference.

View Article



Answer by jake_asks_short_questions for C++ Swapping Pointers

If you are into the dark arts of C I suggest this macro:#define PTR_SWAP(x, y) float* temp = x; x = y; y = temp;So far this has worked for me.

View Article

Answer by zar for C++ Swapping Pointers

The accepted answer by taocp doesn't quite swap pointers either. The following is the correct way to swap pointers.void swap(int **r, int **s){ int *pSwap = *r; *r = *s; *s = pSwap;}int main(){ int *p...

View Article

Answer by Alec Danyshchuk for C++ Swapping Pointers

You are not passing by reference in your example. This version passes by reference,void swap2(int &r, int &s){ int pSwap = r; r = s; s = pSwap; return;}int main(){ int p = 7; int q = 9;...

View Article

Answer by Euro Micelli for C++ Swapping Pointers

You passed references to your values, which are not pointers. So, the compiler creates temporary (int*)'s and passes those to the function.Think about what p and q are: they are variables, which means...

View Article


Answer by Ed Heal for C++ Swapping Pointers

The line r=s is setting a copy of the pointer r to the copy of the pointer s.Instead (if you do not want to use the std:swap) you need to do thisvoid swap(int *r, int *s){ int tmp = *r; *r = *s; *s =...

View Article

Answer by taocp for C++ Swapping Pointers

Inside your swap function, you are just changing the direction of pointers, i.e., change the objects the pointer points to (here, specifically it is the address of the objects p and q). the objects...

View Article

C++ Swapping Pointers

I'm working on a function to swap pointers and I can't figure out why this isn't working. When I print out r and s in the swap function the values are swapped, which leads me to believe I'm...

View Article

Browsing all 8 articles
Browse latest View live




Latest Images