当前位置:网站首页>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.
 Insert picture description here
stay test1() in , We have changed P' The direction of .
 Insert picture description here

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.
 Insert picture description here
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];
 Insert picture description here
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();
}
原网站

版权声明
本文为[stone_ three hundred and twenty-two]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231019083843.html