#include <stdio.h>
#include <string.h>
void swap(int *p1, int *p2)
{
int *temp;
temp=p1;
p1=p2;
p2=temp;
}
main()
{
int n1=10,n2=20;
printf("%d,%d\n",n1++,++n2);
swap(&n1,&n2);
printf("%d,%d",++n1,n2++);
}
When I run this code output is 10,21 and 12,21. My question is why values of N1 and N2 are not swapped? Since function swap uses pointers and method is called by reference shouldn't they swap? Or am I missing some concept? Thanks in advance
当我运行此代码输出是10,21和12,21。我的问题是为什么不交换N1和N2的值?由于函数交换使用指针而方法是通过引用调用它们不应该交换?还是我错过了一些概念?提前致谢
4 个解决方案
#1
4
You've swapped the pointers rather than the values. Remember that C exclusively uses pass-by-value, and so these pointers were passed by value. To swap the things that they refer to you need to de-reference the pointers.
你已经交换了指针而不是值。请记住,C只使用pass-by-value,因此这些指针是按值传递的。要交换它们引用的内容,您需要取消引用指针。
void swap(int *p1, int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
One way to thing about this is that you need to be assigning values of type int
. But your code assigns values of type int*
. And so purely on that analysis which is based only on types, you are able to reject your code.
解决这个问题的一种方法是你需要分配int类型的值。但是您的代码会分配int *类型的值。纯粹基于仅基于类型的分析,您可以拒绝您的代码。
#2
3
void swap(int *p1, int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
#3
0
The pointers you are swapping in your swap() function are local variables of this function. So this swapping has no effect outside this function.
您在swap()函数中交换的指针是此函数的局部变量。所以这个交换在这个函数之外没有任何影响。
To correctly swap values use the code that Wojtek posted.
要正确交换值,请使用Wojtek发布的代码。
You might also want to have have a look at this question: What is the fastest way to swap values in C?
您可能还想看看这个问题:在C中交换值的最快方法是什么?
The answer makes use of an XOR swapping algorithm.
答案使用了XOR交换算法。
#4
0
change your swap function as follow
如下更改交换功能
void swap(int *p1, int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
Note: you are try to make assignment on addresses of variables at your swap
function
注意:您尝试在交换函数的变量地址上进行赋值
#1
4
You've swapped the pointers rather than the values. Remember that C exclusively uses pass-by-value, and so these pointers were passed by value. To swap the things that they refer to you need to de-reference the pointers.
你已经交换了指针而不是值。请记住,C只使用pass-by-value,因此这些指针是按值传递的。要交换它们引用的内容,您需要取消引用指针。
void swap(int *p1, int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
One way to thing about this is that you need to be assigning values of type int
. But your code assigns values of type int*
. And so purely on that analysis which is based only on types, you are able to reject your code.
解决这个问题的一种方法是你需要分配int类型的值。但是您的代码会分配int *类型的值。纯粹基于仅基于类型的分析,您可以拒绝您的代码。
#2
3
void swap(int *p1, int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
#3
0
The pointers you are swapping in your swap() function are local variables of this function. So this swapping has no effect outside this function.
您在swap()函数中交换的指针是此函数的局部变量。所以这个交换在这个函数之外没有任何影响。
To correctly swap values use the code that Wojtek posted.
要正确交换值,请使用Wojtek发布的代码。
You might also want to have have a look at this question: What is the fastest way to swap values in C?
您可能还想看看这个问题:在C中交换值的最快方法是什么?
The answer makes use of an XOR swapping algorithm.
答案使用了XOR交换算法。
#4
0
change your swap function as follow
如下更改交换功能
void swap(int *p1, int *p2)
{
int temp;
temp=*p1;
*p1=*p2;
*p2=temp;
}
Note: you are try to make assignment on addresses of variables at your swap
function
注意:您尝试在交换函数的变量地址上进行赋值