【问题标题】:What happens when I modify the swap function this way?当我以这种方式修改交换函数时会发生什么?
【发布时间】:2020-07-18 07:28:41
【问题描述】:

当我们以这种方式修改交换函数时会发生什么?我知道它不起作用,但到底发生了什么?我不明白我实际上做了什么?

#include <stdio.h>

void swap(int*, int*);

int main(){
   int x=5,y=10;
   swap(&x, &y);
   printf("x:%d,y:%d\n",x,y);
   return 0;
 }

void swap(int *x, int *y){ 
   int* temp;
   temp=x;
   x=y;
   y=temp;
}

【问题讨论】:

  • 这里,您是在本地交换指针本身,而不是它们指向的内容,因此没有外部可观察到的效果。
  • 如果你想让它工作,你需要通过引用传递指针 (int*&amp;);它对于复制成本高昂的东西很有用。
  • @nick 是的,这在特定情况下会很有用,但在这里不可能,因为 xy 的类型是 int,而不是 int*。如果指针不是指针对象,则不能交换指针的值。
  • @RobertSsupportsMonicaCellio 当然,这只是一般技术的一个旁注,即它可以在这个特定设置下工作。我会争辩说,这对整数无论如何都没有意义,因为复制 int 的成本与复制指针的成本相同。
  • 几天前的惊人相似问题:stackoverflow.com/questions/61013632/…

标签: c++ c pointers memory swap


【解决方案1】:

在函数中

void swap(int* x, int* y){ 
    int* temp;
    temp=x;
    x=y;
    y=temp;
}

你只需交换两个函数参数的指针值。

如果你想交换它们的值,你需要像这样实现它

void swap(int* x, int* y){ 
    int temp = *x;  // retrive the value that x poitns to
    *x = *y;        // write the value y points to, to the memory location x points to
    *y = temp;      // write the value of tmp, to the memory location x points to
}

这样,swap 函数会交换引用的内存位置的值。

【讨论】:

  • 不清楚这是否是 OP 想要的,或者他们是否想要交换他们指向的值。
  • @TonyK 我不确定你对我的回答有什么顾虑。我解释了 OPs 交换函数的行为,并提出了一个函数可以做 OP 接缝想要做的事情(公平地说,这只是一个假设。但最可能正确的是 OP 如何使用swap 函数)
  • @TonyK "或者如果他们想交换他们指向的值。" - 但这正是提供的函数所做的。
  • 对不起!我的一个大脑。我的意思是“或者如果他们想自己交换指针”(如尼克对 OP 的评论)。
  • @TonyK "或者如果他们想自己交换指针" 但这在 main 函数 OP 的上下文中没有意义
【解决方案2】:
void swap(int *x, int *y){ 
   int* temp;
   temp = x;
   x = y;
   y = temp;
}

swap() 在这里有两个指向int 的指针,但在内部只交换本地指针xy 的值——它们指向的对象的地址。 - 表示swap()在返回之前的末尾,x指向y已经指向的对象,y指向x已经指向的对象。

xy 指向的对象对调用者没有影响。


如果你想交换xy指向的对象的值,你需要像这样定义swap()

void swap(int *x, int *y){ 
   int temp;
   temp = *x;     // temp gets the int value of the int object x is pointing to.
   *x = *y;       // x gets the int value of the int object y is pointing to.
   *y = temp;     // y gets the int value of temp (formerly x).
}

示例程序(Online Example):

#include <stdio.h> 

void swap(int *x, int *y){ 
   int temp;
   temp = *x;     // temp gets the int value of the int object x is pointing to.
   *x = *y;       // x gets the int value y of the int object y is pointing to.
   *y = temp;     // y gets the int value of temp (formerly x).
}

int main (void)
{
    int a = 1, b = 2;
    printf("Before the swap() function:\n");
    printf("a = %d b = %d\n\n",a,b);

    swap(&a,&b);

    printf("After the swap() function:\n");
    printf("a = %d b = %d",a,b);

    return 0;
}

输出:

Before the swap() function:
a = 1 b = 2

After the swap() function:
a = 2 b = 1

【讨论】:

  • 是的,我明白了!谢谢?
  • @Snape 很高兴能帮到你。
【解决方案3】:

在函数内,它的两个参数交换了传递参数值的副本。

当函数处理参数的副本时,作为临时对象的原始参数(传递给 x 和 y 的指针)将不会被交换。

要交换指向 x 和 y 的指针的值,您必须在 main 中定义这样的指针,通过 x 和 y 的地址初始化它们,并通过指向指针的指针将它们传递给函数。

这是一个演示程序。

#include <stdio.h>

void swap( int **ppx, int **ppy )
{
    int *tmp = *ppx;
    *ppx = *ppy;
    *ppy = tmp;
}

int main(void) 
{
    int x=5, y=10;

    int *px = &x;
    int *py = &y;

    printf( "*px = %d, *py = %d\n", *px, *py );

    swap( &px, &py );

    printf( "*px = %d, *py = %d\n", *px, *py );

    return 0;
}

它的输出是

*px = 5, *py = 10
*px = 10, *py = 5

另请参阅我对这个问题的回答Pointer Confusion: swap method in c

【讨论】:

    猜你喜欢
    • 2018-07-07
    • 2016-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多