M K
M K

Reputation: 317

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 manipulating a copy of which I don't understand because I pass by reference of p and q.

void swap(int *r, int *s)
{
    int *pSwap = r;
    r = s;
    s = pSwap;
    return;
}

int main()
{
    int p = 7;
    int q = 9;  
    swap(&p, &q);
    cout << "p = " << p << "q= " << q << endl;
    return 0;
}

Prints: p = 7q = 9

Upvotes: 21

Views: 90562

Answers (7)

LeeProgrammer
LeeProgrammer

Reputation: 73

void swapPointer(int* &ptr1, int* &ptr2) {
    int* temp = ptr2;
    ptr2 = ptr1;
    ptr1 = temp;
}

It can be resolve by using reference.

Upvotes: 0

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.

Upvotes: -2

zar
zar

Reputation: 12227

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 = new int(7);
    int *q = new int(9);

    cout << "p = " << std::hex << p << std::endl;
    cout << "q = " << std::hex << q << std::endl << std::endl;

    swap(&p, &q);

    cout << "p = " << std::hex << p << std::endl;
    cout << "q = " << std::hex << q << std::endl << std::endl;

    cout << "p = " << *p << " q= " << *q << endl;
    return 0;
}

Output on my machine:

p = 0x2bf6440
q = 0x2bf6460

p = 0x2bf6460
q = 0x2bf6440

p = 9 q= 7

Upvotes: 13

Alec Danyshchuk
Alec Danyshchuk

Reputation: 307

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;
    swap2(p, q);
    cout << "p = " << p << "q= " << q << endl;
    return 0;
}

Passing by reference is not the same as passing by value or by pointer. See C++ tutorials on the web for an explanation. My brain is too small to waste cells storing the fine details I can find on the web easily.

Upvotes: 0

Euro Micelli
Euro Micelli

Reputation: 33998

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 they are slots allocated somewhere in memory (on the stack, but that's not important here). In what sense can you talk about "swapping the pointers"? It's not like you can swap the addresses of the slots.

What you can do is swap the value of two containers that hold the actual addresses - and those are pointers.

If you want to swap pointers, you have to create pointer variables, and pass those to the function.

Like this:

int p = 7;
int q = 9;

int *pptr = &p;
int *qptr = &q;
swap(pptr, qptr);
cout << "p = " << *pptr << "q= " << *qptr << endl;
return 0;

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 59997

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 this

void swap(int *r, int *s)
{
    int tmp = *r;
    *r = *s;
    *s = tmp;
}

Upvotes: 1

taocp
taocp

Reputation: 23634

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 pointed by the pointer are not changed at all.

You can use std::swap directly. Or code your swap function like the following:

void swap(int *r, int *s)
{
   int temp = *r;
   *r = *s;
   *s = temp;
   return;
} 

Upvotes: 28

Related Questions