2016-07-08 63 views
-2

在这里我有两个交换功能变量值如何通过交换函数进行更改?

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

值只有第一函数内改变,
和第二功能永久改变值..

谁能告诉我这两个功能之间的不同? 我以为两个函数都是通过参数指针类型,通过这两个函数值都会被更改。

+0

查找取消引用指针以及它的作用。 –

+1

参数是指针,但它们与它们做不同的事情。确实,阅读什么'int * temp = a;'意味着vs'int temp = * a;' – TheUndeadFish

+1

这个问题可以帮助你理解指针。 [已在这里解决](http://stackoverflow.com/questions/15672805/c-swapping-pointers) – Jason

回答

0

第一个函数交换地址,但不在函数的作用域之外。
第二个函数交换值,并在函数的范围之外。
*添加到名称中,表示您想要的值,而不是它所处的位置。

+1

第一个函数为*调用者*交换* nothing *,这是重要的部分,并且有点一个“交换”功能。 – WhozCraig

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

在功能互换,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指向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现在是6,而num2现在是5。因此在第二个示例中,变量num1num2的值将永久更改。

相关问题