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

时间:2016-07-08 03:01:06

标签: c++ pointers swap

这里我有两个交换功能

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;
}

仅在第一个函数内部更改了值,
第二个功能永久改变值..

谁能告诉我两种功能之间的区别? 我认为两个函数都通过参数获取指针类型,值将通过两个函数来改变..

3 个答案:

答案 0 :(得分:2)

在功能交换中,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仍为5,而num2仍为6。 唯一的问题是a指的是num2b指的是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

已更改。

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

答案 1 :(得分:1)

假设每个函数都被称为:

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
}

答案 2 :(得分:0)

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