In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shares with you the content of the sample analysis of value passing and address passing in C language. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
one。 Value transfer
Let's give an example:
Write a function to find the maximum of the two integers.
# include//get_max function int get_max (int xreint y) {return (x > y)? XRV y;} int main () {int num1 = 10; int num2 = 20; int max = get_max (num1,num2); printf ("max =% d\ n", max); return 0;}
The result of the operation is:
Max = 20
Let's analyze the process of calling this function:
Num1,num2 is passed into the get_max () function as an argument, and the formal parameter XMagi y is instantiated (allocates memory units), and the values of num1 and num2 are passed to x and y correspondingly in the order of the function parameter table, that is, xcircle 10, which is called 20, and then the function returns the value of the larger of x and y. When the function call is complete, x and y are automatically destroyed.
Let's look at the characteristics of the function. If the formal parameter of the function is the same as the actual parameter, this is the value transfer.
two。 Address delivery
Let me give you another example:
Write a function to exchange the contents of two integer variables.
Many beginners think it's too easy, so let's write it again according to the value.
# include / / value passing void Swap1 (int x, int y) {int tmp = 0; tmp = x; x = y; y = tmp;} int main () {int num1 = 1; int num2 = 2; printf ("before exchange:: num1 =% d num2 =% d\ n", num1,num2); Swap1 (num1,num2) Printf ("swap1::num1 =% d num2 =% d\ n", num1, num2); return 0;}
But what is the result at this time?
The value of num1 and Num2 has not changed, it has not been exchanged, why?
Because when the actual parameter is passed to the parameter, the parameter is a temporary copy of the parameter, and the modification of the parameter will not affect the parameter.
Let's print the address of each variable.
You can see that the argument has its own address, and the parameter also has its own address. The argument only passes its own value to the parameter, each with its own address. The value on the address of the parameter has not changed, and the parameter is automatically destroyed after the function is called. That is to say, there is no real relationship between the variables inside and outside the function. Just imagine you copy a bionic man of your own. Did he eat something and get into your stomach? I'm sure he eats him. It has nothing to do with you. (dog head)
So how to solve this problem? Address delivery
# include / / value passing void Swap1 (int x, int y) {int tmp = 0; tmp = x; x = y; y = tmp;} int main () {int num1 = 1; int num2 = 2; printf ("before exchange:: num1 =% d num2 =% d\ n", num1,num2); Swap1 (num1,num2) Printf ("swap1::num1 =% d num2 =% d\ n", num1, num2); return 0;}
Let's take a look at the results
What does address delivery do?
When passing an address, the function parameter is the pointer variable, and the pointer variable contains the address, so the argument passes its address directly, the num1 address in px, the num2 address in py, * px is num1 itself, * py is num2 itself, and the argument itself is assigned and exchanged. This time it is either your imitation stranger or your own experience of life.
Let's take a look at the feature of the function: if the argument passed in is a pointer to the parameter, it is address passing.
In fact, it took me a long time to figure out:
Why did the previous example (returning the larger of the two numbers) succeed without using an address? What is the boundary between these two methods?
Then the question was finally answered:
Because in the first example, the value of num1,num2 does not need to be changed. If the value of x and an is the same as the value of x in the function, it does not affect the result, that is to say, this problem does not need to change the value of the argument, and there is no need to establish such a substantial relationship between the parameter and the parameter.
But to be clear, does the function return num1 itself? Is it the value on the num1 address? No, it's just the value on the x address of the copy of num1.
To sum up, be sure to use address delivery when you need to change the value of the argument.
Thank you for reading! This is the end of the article on "sample Analysis of C language value transfer and address transfer". I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.