如何通过交换函数更改变量的值

how does the value of variable get changed through swap function?

本文关键字:变量 改变 何通过 交换 函数      更新时间:2023-10-16

这里我有两个交换函数

void kswap(int* a, int* b)
{
    int* temp = a;
    a = b;
    b = temp;
}

void kswap(int* a, int* b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

该值仅在第一个函数内部更改,
第二个函数永久更改值。

谁能告诉我两个功能之间的区别?我认为由于两个函数都通过参数采用指针类型,因此值将通过两个函数更改。

在函数交换中,abint *,又名integer pointers,这意味着它们包含内存中整数的地址。如下图所示:

              Memory
          ==================
          +----------------+
          |                |
 +------> |   num1 = 5     |
 |        |                |
 | +----> |   num2 = 6     |
 | |      |                |
 | |      |                |
 | |      |================|
 | |      | Function swap  |
 | |      |                |
 +-(------------ a         |
   |      |                |
   +------------ b         |
          |                |
          +----------------+

这里

`*a`  : should be read as : `value at address contined in a`
`*b`  : should be read as : `value at address contined in b`

在第一个示例中

在第一个kswap,在执行以下语句后,

int* temp = a;  /* A pointer which points to same place as 'a' */
a = b;          /* 'a' will now point to where 'b' is pointing */
b = temp;       /* 'b' will now point to where 'temp' is pointing
                 *  that means where 'a' was previously pointing */

结果是:

               Memory
          ==================
          +----------------+
          |                |
 +------> |   num1 = 5     |  <------+
 |        |                |         |
 | +----> |   num2 = 6     |         |
 | |      |                |         |
 | |      |                |         |
 | |      |================|         |
 | |      | Function swap  |         |
 | |      |                |         |
 + +------------ a         |         |
 |        |                |         |
 +-------------- b         |         |
          |                |         |
          |    temp -----------------+
          +----------------+

请注意,*a*b 都没有分配任何值,因此两者都没有:

`*a`  : that is : `value at address contined in a`
`*b`  : that is : `value at address contined in b`

都变了。

所以如上图所示,num1还在5num2还在6。唯一发生的事情是a指向num2,而b指向num1.

在第二个示例中

在第二个kswap,在执行以下语句后,

int temp = *a;  /* An int variable which will contain the same value as the
                 * value at adress contained in a */
*a = *b;        /* value at address contained in 'a' will be equal to value
                 * at address contained in 'b' */
*b = temp;      /* value at address contained in 'b' will be equal to value
                 * contained in 'temp' */

结果是:

              Memory
         ==================
         +----------------+
         |                |
+------> |   num1 = 6     |
|        |                |
| +----> |   num2 = 5     |
| |      |                |
| |      |                |
| |      |================|
| |      | Function swap  |
| |      |                |
+-(------------ a         |
  |      |                |
  +------------ b         |
         |                |
         |    temp = 5    |
         +----------------+

请注意,*a*b都被分配了新值,因此两者都:

`*a`  : that is : `value at address contained in a`
`*b`  : that is : `value at address contained in b`

都变了。

如上图所示,num1现在是6num2现在是5。因此,在第二个示例中,变量 num1num2 的值将永久更改。

假设每个函数的调用方式为:

void f()
{
    int x = 101, y = 999;    
    kswap(&x, &y);
}

请记住,在C++参数是按值传递的,因此kswap接收x, y所在的地址的值。答案的其余部分内联在下面的代码注释中。

有效的kswap

void kswap(int* a, int* b)
{
    int temp = *a;  // `a` is the address of `int x`
                    // `*a` is the integer value at address `a`
                    //  i.e. the value of `x` so temp == 101 now
    *a = *b;        // same as above `*b` is the value of `y` i.e. 999
                    // now this integer value is copied to the address where `a` points
                    // effectively overwriting the old `x` value `101` with `999`
    *b = temp;      // finally, this copies the value in `temp` i.e. 101
                    // to the address where `b` points and overwrites
                    // the old `y` value `999`, which completes the swap
}

不起作用kswap

void kswap(int* a, int* b)
{
    int* temp = a; // this copies `a` i.e. the address of `x`
                   // to local variable `temp`
    a = b;         // this copies `b` to `a`
                   // since arguments `a` and `b` are pointers and passed by value
                   // this only modifies the value of variable `a`
                   // it does **not** change `x` or its address in any way
    b = temp;      // this copies 'temp' to 'b', same comments as above
                   // now 'a' holds the address of `y` and `b` holds the address
                   // of `x` but **neither** 'x' nor 'y' values have been modified
                   // and pointer variables `a`, `b` go out of scope as soon as
                   // the function returns, so it's all a big no-op in the end
}

第一个函数交换地址,但不在函数范围之外。
第二个函数交换值,并且超出函数的范围。
*添加到名称中意味着您需要该值,而不是它所在的位置。