In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
In C language, the parameters of a function can not only provide detailed data such as integers, decimals, characters, but also pointers to them. Using pointer variables as function parameters, the address inside the function can be passed to the outside of the function, so that the data inside the function can be manipulated outside the function, and the data will not be burned with the completion of the function.
Such as arrays, strings, statically allocated memory and so on are mostly a series of data aggregation, there is no method through a parameter all passed into the outside of the function, can only pass their pointers, outside the function through the pointer to affect these data aggregation.
Sometimes, the operation of fundamental types of data such as integers, decimals, characters, etc., also requires the help of pointers. A typical example is to exchange the values of two variables.
Some beginners can use the following methods to communicate the values of two variables:
# include void swap (int a, int b) {int temp; / / temporary variable temp = a; a = b; b = temp;} int main () {int a = 66, b = 99; swap (a, b); printf ("a =% d, b =% d\ n", a, b); return 0;}
Operational consequences:
A = 66, b = 99
As can be seen from the consequences, the values of an and b have not changed, and the communication has failed. This is because the a, b outside the swap () function and the an and b outside the main () function are divergent variables that occupy different memory. They have no other relationship except the same name. Swap () exchanges the values of its external an and b, which will not affect the values of its internal (main () external) an and b.
After switching to a pointer variable as a parameter, it is easy to deal with the following results:
# include void swap (int * p1, int * p2) {int temp; / / temporary variable temp = * p1; * p1 = * p2; * p2 = temp;} int main () {int a = 66, b = 99; swap (& a, & b); printf ("a =% d, b =% d\ n", a, b); return 0;}
Operational consequences:
A = 99, b = 66
When misappropriating the swap () function, assign the address identification of variables an and b to p1 and p2, so that * p1 and * p2 represent variables an and b themselves, and the values of * p1 and * p2 are the values of AC an and b. It is true that p1 and p2 will be burned after the function is finished, but its influence on the formation of internal an and b is "durable" and will not be "restored" with the completion of the function.
Need to pay attention to the temporary variable temp, its influence is particularly important, because the implementation of * p1 = * p2; the value of an after the statement will be masked by the value of b, if the value of an is not kept first, it will not be found in the future.
Using an array as a function parameter
An array is an aggregation of a series of data that cannot be passed outside the function at once through arguments. If you wish to manipulate the array outside the function, you must pass the array pointer. The following example boundary describes a function max () that is used to find the element with the highest value in the array:
# include int max (int * intArr, int len) {int I, maxValue = intArr [0]; / / it is assumed that the 0th element is the maximum value for (item1; I
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.