The essence of pointer parameter passing and the use of secondary pointer

King of heaven and earth tiger 626 2021-04-11 15:51:53 阅读数:781

本文一共[544]字,预计阅读时长:1分钟~
essence pointer parameter passing use

Level co., LTD. , If there is a mistake , Welcome to correct , thank you .

Let's look at two programs first :

Be patient and watch carefully , I should be able to understand .

1:

void test(char *p)

{

       printf("[test1][p]:%p.\n",p);

       printf("[test2][p]:%s.\n",p);

       p=(char *)malloc(10);

       strcpy(p,"ABCDE");

       printf("[test3]malloc after .....\n");

       printf("[test4][p]:%p.\n",p);

       printf("[test5][p]:%s.\n",p);

       free(p);

}

 

int main()

{

       char b[6] = "abcde";

       char *a = b;

       printf("[main1][a]:%p.\n",a);

       printf("[main2][a]:%s.\n",a);

       test(a);

       printf("[main3][a]:%p.\n",a);

       printf("[main4][a]:%s.\n",a);

       return 0;

}

Output results :                          Be careful :(test Functional pde Value changed ,main Functional a The value of has not changed )

main1][a]:0xbfeaaef6.
[main2][a]:abcde.
[test1][p]:0xbfeaaef6.
[test2][p]:abcde.
[test3]malloc after .....
[test4][p]:0x8a52008.
[test5][p]:ABCDE.
[main3][a]:0xbfeaaef6.
[main4][a]:abcde.

 

2:

void test(char **p)

{

       printf("[test1][p]:%p.\n",p);

       printf("[test2][*p]:%p.\n",*p);

       *p=(char *)malloc(10);

       strcpy(*p,"ABCDE");

       printf("[test3]malloc after .....\n");

       printf("[test4][p]:%p.\n",p);

       printf("[test5][*p]:%p.\n",*p);

       printf("[test6][*p]:%s.\n",*p);

       free(*p);

}

 

 

int main()

{

       char b[6] = "abcde";

       char *a = b;

       printf("[main1][a]:%p.\n",a);

       printf("[main2][a]:%s.\n",a);

       test(&a);

       printf("[main3][a]:%p.\n",a);

       printf("[main4][a]:%s.\n",a);

       return 0;

}

Output results :                          Be careful :(test Functional pde Value changed ,main Functional a The value of has also changed )

[main1][a]:0xbfaca776.
[main2][a]:abcde.
[test1][p]:0xbfaca770.
[test2][*p]:0xbfaca776.
[test3]malloc after .....
[test4][p]:0xbfaca770.
[test5][*p]:0x9132008.
[test6][*p]:ABCDE.
[main3][a]:0x9132008.
[main4][a]:ABCDE.

problem :

1、 The concept of formal parameter and actual parameter ?

2、 The essence of parameter passing ?

3、 What is the pointer ? Why use a pointer to a pointer ?

4、 The first program can't be changed a Value , And the second program can ?

Let's first explain the concepts of formal and actual parameters :

Actual parameters : Real parameters , We defined it ourselves , For example, the pointer in the above program a And an array b It's all arguments , It's all self defined , All parameters defined in curly braces in the basic program are arguments

Shape parameter : When we define a function , Parameters in brackets , For example, in the above program char *p and char **p Medium p Is the parameter , The main purpose is to let the data of the argument be passed into the function , For function operation

The first 2 A question :

          There are two types of parameter passing , One is value passing , The other is citation ; What we're talking about here is mainly value passing , Not to mention reference passing ; There are two kinds of value passing : One is the actual value passing ,int The parameter passing of type belongs to the actual value passing ; The other is address value delivery , The actual parameter is passed to the formal parameter than the actual address , For example, the pointer is the address value transfer .

          Here is the point of parameter passing , When an argument passes an actual value or an address value to a formal parameter , Instead of using arguments directly , It's about opening up memory space on the stack copy One copy ,int a A copy of is _a(_a=a),,char *p A copy of is _p(_p=p), So the operations in the function are all operations on the copy , Changing the value of a formal parameter does not affect the value of an argument , After the function is executed, the space created by the copy is released .

The first 3 A question :

          The concept of pointer , A pointer is also a parameter , and int And char similar ,int Parameters store integers ,char Parameters store characters , The pointer is just an address ; A pointer is the starting address of a piece of memory , If you know this pointer, you can operate on the memory that this pointer points to ; The pointer to the pointer, that is, the secondary pointer, holds the address of the primary pointer , such as :


         p Is the first level pointer , What is kept is a The address of ;q It's a pointer to a pointer ( The secondary pointer ), What's saved is the first level pointer (p) The address of ;q The content of is 0xbfaca770,*q The value of is q Point to 0xbfaca776, namely *q It's still an address , That's the pointer p The content of , namely *q=p,( Make it clear ), Yes *q Operation is right p Point to the memory operation ; Why use secondary pointers ? We'll talk about it next :

Okay , Back to the first 4 A question , The program itself , Why does this happen ?

The first program :

Let's look at the call first test Before and after the function , as well as malloc Before and malloc The pointer after that p And pointer a The address and direction of :

What happened before :( Above the box is the address of the current variable , Inside the box is the value of the current variable )


After that, the situation :     ( Above the box is the address of the current variable , Inside the box is the value of the current variable )

It can be seen from the previous situation that , Function for parameter passing , The argument passes the address to the parameter p(p That is a Copy of (_a),p=_a It's to make it more intuitive , It doesn't produce variable names _a), Two pointers to a piece of memory at the same time ; Use malloc after , Free up new memory and assign the address to p, namely p Change of direction of , Pointing to the new address ; therefore test Internal pair p It's not going to change a Value .

Second procedure :

Again, let's look at the call test Before and after the function , as well as malloc Before and malloc The pointer after that p And pointer a The address and direction of :

What happened before :

After that :

 

          Okay , Let's take a look ,test Function parameters use secondary pointers , We put a My address was sent to p, namely p Yes a; The pointer a It's an array b, That is to say, what is preserved is b The first address , See the first picture of the second program ; The secondary pointer p Point to the first level a, therefore *p The value is a The first address , So change *p It's about change a The content of , That is change a The direction of ; When malloc A piece of memory and save the first address in *p In the content of , Is to put malloc The first address of memory directly replaces the pointer a Original content , therefore a The direction of the pointer has changed , See the second procedure, the second diagram ; So change *p Change a Value ( To understand *p and a It's the same variable ); So if you want to directly operate the pointer passed in by the actual parameter , You can use a secondary pointer , Pass the address of the argument to the secondary pointer , Change the value of the primary pointer through the secondary pointer .
 

版权声明:本文为[King of heaven and earth tiger 626]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2021/04/20210411154919317S.html