如何通过交换函数更改变量的值
how does the value of variable get changed through swap function?
这里我有两个交换函数
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;
}
该值仅在第一个函数内部更改,
第二个函数永久更改值。
谁能告诉我两个功能之间的区别?我认为由于两个函数都通过参数采用指针类型,因此值将通过两个函数更改。
在函数交换中,a
和b
是int *
,又名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
。因此,在第二个示例中,变量 num1
和 num2
的值将永久更改。
假设每个函数的调用方式为:
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
}
第一个函数交换地址,但不在函数范围之外。
第二个函数交换值,并且超出函数的范围。
将*
添加到名称中意味着您需要该值,而不是它所在的位置。
相关文章:
- 变量没有改变?通过向量的函数调用
- 在向量内更改变量的值不会改变其在向量外的值
- C++11:具有互斥锁的线程看到原子变量的值发生变化,尽管这是唯一可以改变它的代码
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 标准在哪里定义了可变变量可以改变
- C++11:可变的 lambda 似乎没有改变变量?
- printf() 似乎改变了一个变量
- 为什么我的变量在单独的线程中修改时没有改变?C++/boost
- 为什么 Player.x 和 Player.y 变量没有改变?
- C - 如何将各处的静态函数称为改变其私有变量
- 即使"friend class rect"在平方类中被评论,为什么它要改变矩形类的私有变量?
- 为什么调用 CUDA 内核函数时这个类成员变量没有改变
- 全局变量的顺序会改变C /OpenGL的性能
- 为什么一个函数的末尾有常量这个词,即使它确实改变了一个变量
- 如何在C++中改变超类和子类的变量
- 为什么变量不会改变
- 变量的范围大于for循环,为什么它不改变值
- 变量似乎在自行改变值
- 在一个case中改变switch变量
- 当赋值给变量时,数字改变值