C++ 交换指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15672805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Swapping Pointers
提问by M K
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.
我正在研究一个交换指针的函数,但我不明白为什么这不起作用。当我在交换函数中打印出 r 和 s 时,这些值被交换了,这让我相信我正在操纵一个我不理解的副本,因为我通过 p 和 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
打印:p = 7q = 9
回答by taocp
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.
在你的swap
函数中,你只是改变指针的方向,即改变指针指向的对象(这里,特别是对象p
和的地址q
)。指针指向的对象根本没有改变。
You can use std::swap
directly. Or code your swap function like the following:
可以std::swap
直接使用。或者像下面这样编码你的交换函数:
void swap(int *r, int *s)
{
int temp = *r;
*r = *s;
*s = temp;
return;
}
回答by zar
The accepted answer by taocp doesn't quite swap pointers either. The following is the correct way to swap pointers.
taocp 接受的答案也没有完全交换指针。以下是交换指针的正确方法。
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
回答by Ed Heal
The line r=s
is setting a copy of the pointer r
to the copy of the pointer s
.
该行r=s
正在将指针r
的副本设置为指针的副本s
。
Instead (if you do not want to use the std:swap) you need to do this
相反(如果你不想使用 std:swap)你需要这样做
void swap(int *r, int *s)
{
int tmp = *r;
*r = *s;
*s = tmp;
}
回答by Euro Micelli
You passed references to your values, which are not pointers. So, the compiler creates temporary (int*)
's and passes those to the function.
您传递了对您的值的引用,这些值不是指针。因此,编译器创建临时(int*)
的并将它们传递给函数。
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.
想想p
和q
是:他们是变量,他们是分配的内存中某个插槽则表示(在栈上,但是这并不重要)。在什么意义上你可以谈论“交换指针”?这不像你可以交换插槽的地址。
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;
回答by Alec Danyshchuk
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.
按引用传递与按值或按指针传递不同。有关解释,请参阅网络上的 C++ 教程。我的大脑太小了,不能浪费细胞来存储我可以在网上轻松找到的精细细节。