当前位置:网站首页>Why does the pointer not change the corresponding value as a formal parameter
Why does the pointer not change the corresponding value as a formal parameter
2022-06-23 10:32:00 【stone_ three hundred and twenty-two】
1 Cited example
Let's first look at such a program , Think about what its output is :
#include <stdio.h>
void test1(int a[], int *p)
{
p = &a[2];
}
void test2()
{
int a[] = {
1,2,3 };
int b = 5;
int* p = &b;
test1(a, p);
printf("%d ", *p);
}
int main(void)
{
test2();
}
You might think , The output of the program is 3, because a[2] = 3. However , The fact is that it outputs 5. stay test1() in , What we pass is a pointer variable , Why would such a result occur ?
2 analysis
2.1 Why is that?
Actually , stay C In language , The actual parameter is through Value passed Of . in other words , A formal parameter is a copy value of an argument .
When a function is called , Will open up space in the stack , Create a variable ( Formal parameter ) Store the value of the argument , You can think , These two variables have the same value , Nothing else . It's like int j = k = 1; equally ,j and k It's just that the values are 1.
We can change the program a little , To verify the above statement .
void test1(int a[], int *p)
{
printf("%p\n", &p); /* Two */
printf("%d\n", *p);
p = &a[2];
}
void test2()
{
int a[] = {
1,2,3 };
int b = 5;
int* p = &b;
printf("%p\n", &p); /* One */
printf("%d\n", *p);
test1(a, p);
printf("%d ", *p);
}
int main(void)
{
test2();
}
Run the program , The result is :
004FFA48
5
004FF974
5
5
It can be seen that , stay One It's about ,p The address for 004FFA48, And in the Two It's about ,p The address for 004FF974, That explains , these two items. p Not the same p. It may be better to understand :
Get into test2() after , We defined the variables manually p, And make it point to b, call test1() when , The program automatically defines variables P', It also points to b.
stay test1() in , We have changed P' The direction of .
test1() After execution ,P' Be released . and P The direction of has not changed .
2.2 How to change
After knowing the reason , How to change the program , To achieve the effect we want ? here , We used a double pointer . Look directly at the program :
void test1(int a[], int **p)
{
printf("%p\n", &p); /* Two */
printf("%p\n", &*p); /* 3、 ... and */
*p = &a[2]; /* Four If you want to change b Value , Just change this to **p = a[2];*/
}
void test2()
{
int a[] = {
1,2,3 };
int b = 5;
int* p = &b;
printf("%p\n", &p); /* One */
printf("%d\n", *p);
test1(a, &p);
printf("%d ", *p);
}
int main(void)
{
test2();
}
Run the program :
0095FB84
5
0095FAB0
0095FB84
3
As you can see from the results , 3、 ... and Place and One The output at is the same , So it can achieve the desired effect . alike , Use the method of illustration to understand :
Get into test2(), Definition P Point to b, function test1(), Create temporary variables P' Point to P, It can be understood as int **P' = &P.
test1() in ,*p = &a[2]; in ,*p That is *p', That is to say P Value , therefore *p = &a[2]; Equivalent to P = %a[2];
test1() After execution ,P' Be released ,*P Change into 3.
3 Why is it wrong
why “ Taken for granted ” To make mistakes in the cited examples ? The source may be the guide when the array is used as a formal parameter . Let's take another example :
#include <stdio.h>
void test3(int a[])
{
a[2] = 5;
}
void test4()
{
int a[] = {
1,2,3 };
test3(a);
printf("%d\n", a[2]);
}
int main()
{
test4();
}
There are not so many patterns , The output is really 5. Array names are also pointers , Why does it work ? Isn't it value passing ?
The answer is , It is also value passing , however , The situation here is slightly different from the previous example . Here is another example , Readers can Combined with the following example , Create as before P' And the method of drawing , Think for yourself .
#include <stdio.h>
void test4()
{
int a[] = {
1,2,3 };
int* b = a;
b[2] = 5;
printf("%d\n", a[2]);
}
int main()
{
test4();
}
边栏推荐
- Google Earth Engine(GEE)——GEDI L2A Vector Canopy Top Height (Ver
- 为什么poll/select在open时要使用非阻塞NONBLOCK
- Data structures and differences between MySQL InnoDB engine and MyISAM
- JVM easy start-01
- 数值计算方法
- Noi OJ 1.3 20: power C language for computing 2
- list的深度剖析及模拟实现
- 2021-04-16 recursion
- 解决audio自动播放无效问题
- Noi OJ 1.3 16: calculating segment length C language
猜你喜欢

Several practical software sharing

Too helpless! Microsoft stopped selling AI emotion recognition and other technologies, saying frankly: "the law can not keep up with the development of AI"

Solve the problem of invalid audio autoplay

六张图详解LinkedList 源码解析

用贪吃蛇小游戏表白(附源码)

Mathematical analysis_ Notes_ Chapter 2: real and plural numbers

JVM easy start-02

MySQL基础-笔记

Build a QQ robot to wake up your girlfriend

详解判断大小端的方法
随机推荐
R和RStudio下载安装详细步骤
春招面试经验汇总(技术岗)
2021-04-16递归
NOI OJ 1.3 14:大象喝水 C语言
2021-05-10 method rewrite polymorphism considerations
What does NFTs, Web3 and metauniverse mean for digital marketing?
Noi OJ 1.2 conversion between integer and Boolean C language
SQL create a new field based on the comparison date
实现常用C语言字符串处理函数
Golang 快速上手 (1)
Cool photo album code, happy birthday to the object!
Implementing Domain Driven Design - using ABP framework - General guidelines
Nuxt. Differences between JS spa and SSR
2021-05-07 package inheritance super this
2021-05-10方法重写多态注意事项
5 login failures, limiting login practice
NOI OJ 1.3 20:计算2的幂 C语言
技术创造价值,手把手教你薅羊毛篇
list的介绍及使用
2021-04-27 classes and objects