这是C代码。
#includevoid func(int *a){ *a = 2; int c = 3; int *b = &c; a = b; *a = 3; }void main(){ int a = 1; func(&a); printf("%d\n", a); }
运行结果是
2
为什么a的结果不是3呢?
Your Answer
1 answers
在func函数中,你可以将int *a写成int* a,这样就一目了然了,a是形参。一开始,a的值是main函数里a的地址。这样我们把func形参a换个名字,就叫做d吧。这样整个函数就变成这样了:
void func(int* d){
*d = 2;
int c = 3; int* b = &c;
d = b;
*d = 3;
}
好了我们再来看。一开始d是a的地址。所以*d赋值就是给a赋值。但是,b是c的地址,d = b,d就变成c的地址了,那么给*d赋值就是给c赋值了,和a就没有什么关系了。所以只被赋值了第一次。
不知道这样你懂了没,我不善表达,见谅。
Hot tools Tags
Hot Questions
convert_tz returns null
2026-01-02 22:43:29
How can I remove a key from a Python dictionary?
2026-01-02 22:22:08
How to use generics in Unmarshal (go 1.18)
2026-01-02 22:01:13
PHP 5.4 Call-time pass-by-reference - Easy fix available?
2026-01-02 21:43:08
How to create a new object instance from a Type
2026-01-02 21:22:41
Popular tool
vc9-vc14 (32+64 bit) runtime library collection (link below)
Download the collection of runtime libraries required for phpStudy installation
VC9 32-bit
VC9 32-bit phpstudy integrated installation environment runtime library
PHP programmer toolbox full version
Programmer Toolbox v1.0 PHP Integrated Environment
VC11 32-bit
VC11 32-bit phpstudy integrated installation environment runtime library
SublimeText3 Chinese version
Chinese version, very easy to use
Hot Topics
Douyin level price list 1-75
20416
7
20416
7
wifi shows no ip assigned
13576
4
13576
4
Hot Article
How to set up price alerts so you don't miss key entry points?
2026-01-01
By DDD
Why do professional traders advise newbies to start with low leverage?
2026-01-01
By DDD
Tutorial on existence check of nested array values in PHP multidimensional array
2025-12-28
By DDD
How to draw dotted lines in PS How to draw various dotted lines in PS
2025-12-28
By DDD






