为什么即使我使用指针,我的代码也不会交换变量的值? [重复]

时间:2021-12-26 20:20:47

This question already has an answer here:

这个问题在这里已有答案:

#include<stdio.h>
void mystery(int *ptra, int *ptrb) 
{
   int *temp;
   temp = ptrb;
   ptrb = ptra;
   ptra = temp;
}
int main() 
{
    int a=2016, b=0, c=4, d=42;
    mystery(&a, &b);
    if (a < c)
        mystery(&c, &a);
    mystery(&a, &d);
    printf("%d\n", a);
}

I am attempting to swap the value of the variables by making use of pointers but I fail to understand why the variables contain the same value even after the function calls

我试图通过使用指针交换变量的值,但我无法理解为什么即使在函数调用后变量包含相同的值

3 个解决方案

#1


4  

You need to change content being pointed to by the pointers, not the pointers themselves.

您需要更改指针指向的内容,而不是指针本身。

void mystery(int *ptra, int *ptrb) 
{
   int temp; //make it non-pointer also.
   temp = *ptrb;
   *ptrb = *ptra;
   *ptra = temp;
}

If you change the pointer themselves, the change will be local to the function, because the variables ptra and ptrb are local variables, holding the addresses.

如果您自己更改指针,则更改将是函数的本地更改,因为变量ptra和ptrb是局部变量,保存地址。

Remember, in general, that when you deal with a pointer, there is usually two objects involved:

一般来说,请记住,当您处理指针时,通常会涉及两个对象:

  • the pointer itself
  • 指针本身

  • the object which the pointer points to, i.e the content.
  • 指针指向的对象,即内容。

In your case, you're dealing with the pointers only, not the contents — and the pointers themselves are passed by value, which is why the changes to pointer variables are local to the function.

在你的情况下,你只处理指针,而不是内容 - 并且指针本身是通过值传递的,这就是指针变量的变化是函数本地的原因。

Also, note that there is already std::swap which does the job. In case you're unaware of it, use it instead.

另外,请注意已经有std :: swap来完成这项工作。如果你不知道它,请改用它。

#2


1  

Inside your mystery function, the notation you'd have needed to use to actually access the values to which the pointers point is:

在你的神秘函数中,你需要用来实际访问指针指向的值的符号是:

*ptra

That's called dereferencing the pointer. I have an old answer about that here.

这被称为取消引用指针。我在这里有一个旧答案。

So:

void mystery(int *ptra, int *ptrb) 
{
   int temp = *ptrb;
   *ptrb = *ptra;
   *ptra = temp;
}

#3


0  

You are not swapping the values of the variable pointed by the pointer but you are swapping the addresses instead.

您没有交换指针指向的变量的值,而是交换地址。

use * beside the pointer to use the value instead of address.

在指针旁边使用*来使用值而不是地址。

#1


4  

You need to change content being pointed to by the pointers, not the pointers themselves.

您需要更改指针指向的内容,而不是指针本身。

void mystery(int *ptra, int *ptrb) 
{
   int temp; //make it non-pointer also.
   temp = *ptrb;
   *ptrb = *ptra;
   *ptra = temp;
}

If you change the pointer themselves, the change will be local to the function, because the variables ptra and ptrb are local variables, holding the addresses.

如果您自己更改指针,则更改将是函数的本地更改,因为变量ptra和ptrb是局部变量,保存地址。

Remember, in general, that when you deal with a pointer, there is usually two objects involved:

一般来说,请记住,当您处理指针时,通常会涉及两个对象:

  • the pointer itself
  • 指针本身

  • the object which the pointer points to, i.e the content.
  • 指针指向的对象,即内容。

In your case, you're dealing with the pointers only, not the contents — and the pointers themselves are passed by value, which is why the changes to pointer variables are local to the function.

在你的情况下,你只处理指针,而不是内容 - 并且指针本身是通过值传递的,这就是指针变量的变化是函数本地的原因。

Also, note that there is already std::swap which does the job. In case you're unaware of it, use it instead.

另外,请注意已经有std :: swap来完成这项工作。如果你不知道它,请改用它。

#2


1  

Inside your mystery function, the notation you'd have needed to use to actually access the values to which the pointers point is:

在你的神秘函数中,你需要用来实际访问指针指向的值的符号是:

*ptra

That's called dereferencing the pointer. I have an old answer about that here.

这被称为取消引用指针。我在这里有一个旧答案。

So:

void mystery(int *ptra, int *ptrb) 
{
   int temp = *ptrb;
   *ptrb = *ptra;
   *ptra = temp;
}

#3


0  

You are not swapping the values of the variable pointed by the pointer but you are swapping the addresses instead.

您没有交换指针指向的变量的值,而是交换地址。

use * beside the pointer to use the value instead of address.

在指针旁边使用*来使用值而不是地址。