考虑一下这个功能:
void swap (int *p,int *q)
{
int temp;
temp=*p;
*p=*q;
*q=temp;
}
swap(4 , 5)
输出:
5 4
我知道这是使用指针交换两个整数的正确方法,如书中所述。但那里到底发生了什么?这很令人困惑。考虑这个函数,其中step * p = * q被p = q替换,即删除了间接运算符*。
void swap (int *p,int *q)
{
int temp;
temp=*p;
p=q;
*q=temp;
}
现在,会发生什么?在这种情况下,我得到的输出
swap(4 , 5)
输出:
4 4
答案 0 :(得分:4)
将int
视为一个带有数字的框,并将指针视为剪贴板上的条目,告诉您要查看哪个框。
当你这样做时
*p = *q;
您正在列表中查找p
应使用的框,以及q
应使用的框;然后,您查看q
框中的数字,并将其复制到p
框中。您仍然有两个单独的框,并在剪贴板上有两个单独的条目;它只是两个盒子中的数字相同。
但是当你做的时候
p = q;
你完全不接触盒子了。您只需要更改剪贴板。您正在查找应该用于q
的框,然后您清除剪贴板上p
的条目并将其替换为您在q
下找到的相同条目。现在你有两个盒子,里面有不同的数字;但是您的剪贴板包含两个相同的条目,并且都指向同一个框。您的其中一个框现在无法访问(您的剪贴板上没有指向您的条目),并且当您查找p
或q
时,您最终会结束查看同一个框。
(值得注意的是,此剪贴板仅用于执行此特定功能,之后会被丢弃。您对作为参数传递的指针所做的更改将赢得'在函数执行完毕后会产生任何影响。但是在你开始担心范围之前,我首先要解决其他概念。)
答案 1 :(得分:2)
因为您已删除 int
5
移至*p
的步骤,因此您只需移动{ {1}} 4
通过*q
。
答案 2 :(得分:1)
请注意int *p
和int *q
是指针(表示内存地址!!)。所以,当你写p=q
时,你最终得到两个指向同一地址的指针!!!
void swap (int *p,int *q)
{
int temp;
temp=*p; // temp = 4
p=q; // p points at the same address with q (where 5 is stored)
*q=temp; // store 4 where q points
}
您现在明白两个指针都指向存储整数4的内存位置!两个指针都指向相同的内存位置。
答案 3 :(得分:1)
一般来说,用指针来理解代码中发生的事情的最好方法是拿出一支铅笔和一些纸张并开始绘图。
我使用我的电脑,这很多,很多慢。
由于swap(4,5)
实际上不起作用,我假设代码中有两个变量调用swap
:
int x = 4;
int y = 5;
swap(&x, &y);
swap
的前两行在两个版本中都是相同的,因此前两张图片适用于两者。
当您输入swap
时,它将如下所示:
如您所见,p
指向x
,q
指向y
。
temp
的值是不确定的。
现在我们已经关注p
指针(*p
)并将x
的值复制到temp
。
版本一:
在这里,我们按照指针q
(*q
),获取y
的值(即5),然后我们按照指针p
({{1并将该值存储到*p
中。
最后,我们关注x
,因此我们可以在q
中存储temp
的值,即x
曾经拥有的值。
第二版
在此,我们将y
的值复制到q
。
由于p
和p
是指针,因此它们指向相同的内容(q
)。
如您所见,变量y
没有改变;我们刚刚更改了x
,所以它指向其他地方。
此步骤与第一个版本完全相同:通过跟随p
将temp
的值存储在y
中。
希望这更清楚。
答案 4 :(得分:0)
您的程序会崩溃,因为您传递的是swap(4,5);
4
5
和p
q
和void swap(int *p,int *q){
......
}
指向的常量p
和q
的地址?
{{1}} 期望指针{{1}}和{{1}}指向的两个变量。
答案 5 :(得分:0)
将此代码复制到代码编辑器并测试运行它。尝试使用注释了解此代码的作用。
//Start from main() function
#include <iostream>
using namespace std;
// We are here from main function
/*
This function takes two arguments
both are pointer of type int. When declaring
a pointer of type int we write "int *x". Which means
that the vaiable x is pointer and it can only hold
an address of a varible instead of it's value.
However, We now have addresses of p in x and
q in y. We want to exchange values but taking
addresses of them instead. This is a bit confusing.
*/
void swap(int *x, int *y)
{
int temp = 0;
/*
Things are more confusing in the line below.
" * " is a multipurpose operator.
1. When you put this operator in the middle
of two variable (like a*b ) then it works as a
multipliyer.
2. When you put it when declaring a variable
( like int *x ) then it works as if you are
declaring a pointer of type int.
3. When you put it before a pointer variable name but not
declaring the pointer variable then it gives the
value the pointer pointing to.
In our case the third statement is happening. As x has
the address of p, *x means the value of p.
*/
temp = *x; // Here, *x = 10 (because p = 10) so temp = 10.
/*
Here, we are replacing the value of *x (or p, which is 10)
by *y (or q which is 20).
So, after the execution of the line below
we will have *x = 20 or p = 20. It means we are losing
the previous value of p. This is the reason we kept the value
of p in temp.
*/
*x = *y; // Now p = 20 and q = 20
/*
The line below sets *y (or q ) to the value of temp
(which is 10).
*/
*y = temp; //Now p = 20 and q = 10
}
int main()
{
int p = 0;
int q = 0;
cout << "Enter p : ";
cin >> p; // Say you have entered 10. Now p = 10
cout << "Enter q : ";
cin >> q; // Say you have entered 20. Now q = 20
/*
This line takes you to the swap function above.
We are not sending p, q here. We are sending pointer
or addresses of p and q.
*/
swap(&p, &q); // This line takes you to the swap function above.
cout << "p : " << p << endl;
cout << "q : " << q << endl;
return 0;
}
答案 6 :(得分:-1)
正常工作的功能:
void swap (int *p,int *q)
{
int temp; //You take an integer variable
temp=*p; //You store the value pointed to by p to temp which is 4
*p=*q; //You copy the value pointed to by q to p which is 5, hence p points to 5
*q=temp; //You store the value in temp as the value pointed by q, hence q points to 4 as temp holds 4
}
现在代码中包含您的更改:
void swap (int *p,int *q)
{
int temp;
temp=*p; //You store the value pointed to by p to temp which is 4
p=q; //p and q are addresses, you equate them so the value pointed becomes equal, if you make change in one the other changes automatically. So basically the value of 'p' in the calling function doesn't change, the change is a local change as the arguments to the called function are passed by reference. So the calue pointed to by p is not altered in 'swap' function.
*q=temp; //Now to make the value pointed to by q as 4
}
答案 7 :(得分:-4)
您可以使用xor交换算法:
#include <stdio.h>
int main(int argc, char** argv)
{
int i = 1;
int j = 2;
int *p = &i;
int *q = &j;
printf("BEFORE: %d - %d\n", *p, *q);
if (*p != *q)
{
/* swap */
*p ^= *q;
*q ^= *p;
*p ^= *q;
}
printf("AFTER: %d - %d\n", *p, *q);
return 0;
}