Reference difference parameter type pointer and a pointer function

In fact, the final analysis is passed by value and pass by reference different, but related to the pointer, could understand it a little more complicated, we can go take a look by value and pass by reference Bowen area.

First look at a simple function

In two breakpoint value p1 respectively

 

with

 

Then write yourself a handy function fun1, call it in the main function

 

We originally expected that: by applying for a store fun1 function int memory, then let pass into fun1 function pointer to memory just application.

But the final result of two break point is this:

After the execution fun1 function, value p1 is still empty , that is to say our expectations were not successful.

To achieve our original expectations, it can be divided into two steps :

The first step: new a memory

Step Two: Let us just new pointer to out of memory.

In fact, fun1 function is indeed a new memory , but did not do let us pass in a pointer to the new out of memory, so the value of p1 pointer and it did not change (the value of p1 is the memory address pointed to by p1) .

Why is this?

Here it involves the formal and actual parameters , traditional values, and pass references to the concept of:

In short, when the function of the parameter type we are not referenced when, as we argument (p1 main function above) pass into the function value of a function parameter (p fun1 function) is obtained by the argument copy obtained , i.e. the value at this time and parameter arguments are the same, are empty, but they are two different things ,

It merely points to the same memory, in addition, it does not have any contact.

Then, fun1 new statement in the function body, then the effect is to make the new parameter p points out of memory , and your arguments (p1) is not half dime , p1 originally what is now or something.

We can hit a breakpoint in fun1 function to verify this:

At the beginning: the same value p1 and p.

We can see the value of the parameter p really changed.

 

Then turn-reference: when the function is parameter reference type, when the arguments passed into the function, the parameter is not only for a simple replication , parameter arguments alias become directly , that is to say this when the formal and actual parameters is a thing that you operate on the formal parameters, will be reflected in real time to the argument body . We can write a fun2 function, the function parameter type to a reference type, can achieve the effect of a thought, as:

 

ps: the above code I have just a little new, but not the Delete , this tube to kill regardless of buried behavior, we do not imitate!

 

Guess you like

Origin www.cnblogs.com/XiaoXiaoShuai-/p/10958583.html