当前位置:网站首页>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();
}
边栏推荐
- Mysql 的Innodb引擎和Myisam数据结构和区别
- 技术创造价值,手把手教你薅羊毛篇
- Parity of UART
- How to write a literature review? What should I do if I don't have a clue?
- MySQL Basics - Notes
- NOI OJ 1.2 10:Hello, World! Size of C language
- 圖片存儲--引用
- JVM easy start-02
- Unity technical manual - shape sub module - Sprite, spriterenderer and velocity over lifetime
- SQL create a new field based on the comparison date
猜你喜欢

Set up a QQ robot for ordering songs, and watch beautiful women

Pycharm installation tutorial, super detailed

Golang 快速上手 (1)

SQL教程之SQL 中数据透视表的不同方法

当 Pandas 遇见 SQL,一个强大的工具库诞生了

【软件与系统安全】堆溢出

Nuxt. Differences between JS spa and SSR

Experience of using thread pool in project

Golang quick start (3)
![[software and system security] heap overflow](/img/ca/1b98bcdf006f90cabf3e90e416f7f2.png)
[software and system security] heap overflow
随机推荐
Too helpless! Microsoft stopped selling AI emotion recognition and other technologies, saying frankly: "the law can not keep up with the development of AI"
炫酷相册代码,祝对象生日快乐!
Noi OJ 1.2 06: round floating point numbers to zero
Experience of using thread pool in project
【第23天】给定一个长度为 n 的数组,将元素 X 插入数组指定的位置 | 数组插入操作4
2021-04-16 array
Mysql-03. Experience of SQL optimization in work
SPI与IIC异同
Noi OJ 1.3 15: apple and bug C language
Noi OJ 1.3 13: reverse output of a three digit C language
实现领域驱动设计 - 使用ABP框架 - 通用准则
Spring recruitment interview experience summary (technical post)
R和RStudio下载安装详细步骤
2021-05-11 static keyword
Noi OJ 1.2 conversion between integer and Boolean C language
Mysql-03.工作中对SQL优化的心得体会
2021-05-12内部类
Five SQL functions for operation date that must be known in SQL tutorial
Cool photo album code, happy birthday to the object!
JVM easy start-02