The problem of adding & to function input variable

Preface

When learning the data structure, I found that there are non-pointer variables passed by the function, but an & is added in front of the variable. Examples are as follows:

Pop(&S, &e) //e为整型量

Very strange for this kind of operation. I checked the reason on the Internet and got the following results


the reason

It turns out that in the C++ language, the type is added with & in front to indicate a reference, such as int a; and int & b = a; where b is added with & in front to indicate that b refers to a, and the two values ​​are equal, and changing b will change a. In the function When passing a parameter, adding & means that the actual parameter is directly passed to the function as a "formal parameter", the operation in the function directly changes the value of the actual parameter, without adding & means that it is the copy value of the passed actual parameter, this value is another Open up space for storage, his changes will not affect the original actual parameters, so adding & can flexibly change whether to change the value of the incoming parameter, making the algorithm calculation more convenient!


to sum up

Own too much

Guess you like

Origin blog.csdn.net/qq_45396672/article/details/111183402