C++中指针地址的交换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1826203/
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
Swapping addresses of pointers in C++
提问by There is nothing we can do
How can one swap pointer addresses within a function with a signature?
如何在具有签名的函数中交换指针地址?
Let's say:
让我们说:
int weight, height;
void swap(int* a, int* b);
So after going out of this function the addresses of the actual parameters (weight
and height
) would be changed. Is it possible at all?
所以退出这个函数后,实际参数(weight
和height
)的地址会改变。有可能吗?
回答by UncleBens
If you want to swap the addresses that the pointers are pointing to, not just the values stored at that address, you'll need to pass the pointers by reference (or pointer to pointer).
如果要交换指针指向的地址,而不仅仅是存储在该地址的值,则需要通过引用(或指向指针的指针)传递指针。
#include <cassert>
void swap(int*& a, int*& b)
{
int* c = a;
a = b;
b = c;
}
int main()
{
int a, b;
int* pa = &a;
int* pb = &b;
swap(pa, pb);
assert(pa == &b); //pa now stores the address of b
assert(pb == &a); //pb now stores the address of a
}
Or you can use the STL swap function and pass it the pointers.
或者您可以使用 STL 交换函数并将指针传递给它。
#include <algorithm>
std::swap(pa, pb);
Your question doesn't seem very clear, though.
不过,你的问题似乎不是很清楚。
回答by RED SOFT ADAIR
In C++ you would write
在 C++ 中,你会写
void swap(int *a, int *b)
{
std::swap(*a, *b);
}
or rather just use:
或者只是使用:
std::swap(a, b);
回答by Toad
The new answer(since the question has been reformulated)
新答案(因为问题已重新表述)
is that addressed of variables are determined at compile time and can therefore not be swapped. Pointers to variables however can be swapped.
是变量的地址是在编译时确定的,因此不能交换。然而,指向变量的指针可以交换。
Old answer:this was the answer when the question still implied swapping the values of 2 variables by means of a function:
旧答案:当问题仍然暗示通过函数交换 2 个变量的值时,这是答案:
function call:
函数调用:
int width=10, height=20;
swap(&width, &height)
implementation:
执行:
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a = *b;
*b = temp;
}
or...without using a temporary variable: ;^)
或者...不使用临时变量:;^)
void swap(int *a, int *b)
{
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
do watch out that the last method breaks for the case: swap (&a, &a). Very sharply pointed out by user9876 in the comments.
请注意最后一个方法在这种情况下会中断:swap (&a, &a)。user9876 在评论中非常尖锐地指出。
回答by MSalters
It seems you might be a bit confused about terms.
您似乎对术语有些困惑。
An object usually has an address. That is the location in memory where the object is located. Some temporary objects don't have addresses, because they don't need to be stored. Such an exception is the temporary "4" object in the expression (2+2)*3
一个对象通常有一个地址。那就是对象所在的内存位置。一些临时对象没有地址,因为它们不需要存储。这样的例外是表达式中的临时“4”对象(2+2)*3
A pointer is an object that stores an address of another object. Hence, for every type, there is a matching pointer. int
has int*
, std::string
has std::string*
, etcetera.
指针是存储另一个对象地址的对象。因此,对于每种类型,都有一个匹配的指针。int
有int*
,std::string
有std::string*
,等等。
Now, you write about "addresses of pointers". They exist. After all, I wrote that a pointer is an object, and thus it has its own address. And you can store that address in another pointer. For instance, you can store the address of and int*
in an int* *
. But do you really intended that? Or did you mean the "address referencedby a pointer"?
现在,您将写到“指针地址”。他们存在。毕竟,我写过指针是一个对象,因此它有自己的地址。您可以将该地址存储在另一个指针中。例如,您可以将 和 的地址存储int*
在int* *
. 但你真的是故意的吗?或者你的意思是“指针引用的地址”?
Now, you give height and weight as examples. The standard way to swap them in C++ is simply std::swap(width, height)
. Note the std::
, which is the prefix for C++ standard library functions. std::swap
will swap almost everything. ints, floats, wives. (j/k).
现在,您以身高和体重为例。在 C++ 中交换它们的标准方法很简单std::swap(width, height)
。请注意std::
,它是 C++ 标准库函数的前缀。std::swap
将交换几乎所有东西。整数,浮点数,妻子。(j/k)。
You have another swap function, apparently. It accepts two pointers to integers, which means it wants the addresses of two integers. Those are easy to provide, in this case. width
is an integer, and &width
is its address. That can be stored in the pointer argument int* a
. Similarly, you can store the address &height
in argument int*b
. Putting it together, you get the call swap(&width, &height);
显然,您还有另一个交换功能。它接受两个指向整数的指针,这意味着它需要两个整数的地址。在这种情况下,这些很容易提供。width
是一个整数,&width
是它的地址。这可以存储在指针参数中int* a
。同样,您可以将地址存储&height
在参数中int*b
。把它放在一起,你会接到电话swap(&width, &height);
How does this work? The swap(int*a, int*b)
function has two pointers, holding the address of two integers in memory. So, what it can do is [1] set aside a copy of the first integer, [2] copy the second integer in the memory where the first integer was, and [3] copy the first integer back in the memory where the second integer was. In code:
这是如何运作的?该swap(int*a, int*b)
函数有两个指针,保存内存中两个整数的地址。因此,它可以做的是 [1] 留出第一个整数的副本,[2] 将第二个整数复制到第一个整数所在的内存中,并 [3] 将第一个整数复制回第二个整数所在的内存中整数是。在代码中:
void swap(int *a, int *b)
{
int temp = *a; // 1
*a = *b; // 2
*b = temp; // 3
}
回答by LostMohican
If you want to change address of pointers then you have to pass pointers of those pointers as your parameters:
如果要更改指针的地址,则必须将这些指针的指针作为参数传递:
void swap(int **a, int **b); //as prototype
Those examples are just changes values of pointers.
这些例子只是改变指针的值。