How to build a function in C language to exchange the values of two variables (detailed, easy to understand)

        First of all, we need to know the principle of exchanging the values ​​of two variables. In the C language code, = represents the assignment operation, so it is not a=b, b=a is enough , because when you write a=b, a and b are both the value of b, and then let b=a, the two numbers are still the value of b, and there is no way to exchange them.

        Therefore, we need to set a third variable to provide a "temporary help" effect. For example, re-declare a variable of c, let c=a, a=b, b=c , at this time the values ​​of a and b are exchanged . When c=a, the values ​​of a and c are both a, and when a=b, a is b, which is half the success. Finally, let b=c, and c has been assigned by a in the first step, so b becomes the value of a.

        For example: there are two full cups of liquid, a cup of magma, and a cup of ice water, and the two of them want to exchange positions. We can only take another cup to hold one of the cups of liquid first, and then exchange.

        After understanding the principle of two value exchange, we can start to solve this problem. Use a function to exchange, that is, customize a function outside the main function, so it is not the same as the ordinary c=a a=bb=c. In this question, we cannot directly transfer the actual parameter (that is, abc in the main function), because the formal parameter (abc in the custom function) is only a temporary copy of the actual parameter (abc in the main function) , just like cloning Dolly the sheep, whether Dolly lives or dies will not affect the original sheep. Changing the formal parameters in the function will not affect the actual parameters at all , so what should we do? (The figure below is an error demonstration of transferring actual parameters. You can see that 4 and 5 in the output window have not been exchanged)

        Since call by value cannot be used, we can use the method of taking the address to change the function from call by value to call by address .

        Add the address character & to the passing value in the function, and *pa, *pb (pointer symbol) in the function change (the function name can be customized), and the addresses of a and b can be transferred to the function. In this way, you can find a and b in the main function through pointers, and directly change a and b . (The following is a correct demonstration, that is, call by reference)

        Finally, not all functions are suitable for call-by-reference . For example, a function that calculates a larger value returns the same value as the original larger value required in the main function. Call-by-value is relatively simpler and more intuitive, so there is no need to use call-by-reference.

        The above is the whole content of this blog. Due to the lack of knowledge of the blogger, it is inevitable that there will be mistakes. I hope that the big guys can give me some advice. You can correct me in the comment area or private message. The blogger must correct it in time

        So see you next time~~

Guess you like

Origin blog.csdn.net/weixin_70218204/article/details/127132164