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--
I would like to share with you what pointer function and function pointer mean. I hope you will get something after reading this article. Let's discuss it together.
1. A pointer function is a function with a pointer, that is, it is essentially a function. The function return type is a pointer to a certain type
Type identifier
* function name (parameter table)
Int * f (XBI y)
First of all, it is a function, but the return value of this function is an address value. The function return value must be accepted with a pointer variable of the same type, that is, the pointer function must have a function return value, and, in the main tone function, the function return value must be assigned to the pointer variable of the same type.
Indicates:
Float * fun ()
Float * p
P = fun (a)
Pay attention to the difference between the pointer function and the function pointer representation, do not confuse. The easiest way to tell is to see if the pointer * sign in front of the function name is contained in parentheses (). If it is included, it is a function pointer, and vice versa.
Let's be more specific. Please look at the following
Pointer function:
When a function declares its return value as a pointer, it actually returns an address to the calling function for use in expressions that require a pointer or address.
Format:
Type specifier * function name (parameter)
Of course, since an address is returned, the type specifier is usually int.
For example: int * GetDate ()
Int * aaa (int,int)
Function returns an address value, which is often used on the address of an element in the returned array.
Int * GetDate (int wk,int dy)
Main ()
{
Int wk,dy
Do
{
Printf (Enter week (1-5) day (1-7)\ n)
Scanf (% d% drecoverwkjeddy)
}
While (wk5 | | dy7)
Printf (% d\ nGetDate (wk,dy))
}
Int * GetDate (int wk,int dy)
{
Static int calendar [5] [7] =
{
{1,2,3,4,5,6,7}
{8,9,10,11,12,13,14}
{15,16,17,18,19,20,21}
{22,23,24,25,26,27,28}
{29pcr 30pcr 31mlle lime1}
}
Return & Germanar [wk-1] [dy-1]
}
The program should be easy to understand, and the subfunction returns the address of an element in the array. The output is the value in this address.
2. A function pointer is a pointer variable pointing to a function, that is, it is essentially a pointer variable.
Int (* f) (int x); / * declare a function pointer * /
Assign the first address of the func function to pointer f
, /
The pointer to the function contains the address of the function, through which you can call the function. The format of the declaration is as follows:
Type specifier (* function name) (parameter)
In fact, it can not be called the function name, but should be called the variable name of the pointer. This special pointer points to a function that returns an integer value. The declaration stroke of the pointer is consistent with its declaration to the function.
The parentheses outside the pointer name and pointer operator change the default operator priority. Without parentheses, it becomes a prototype declaration of a function that returns an integer pointer.
For example:
Void (* fptr) ()
You can assign the address of a function to a function pointer in the following two forms:
Fptr=&Function
Fptr=Function
Taking the address operator is not necessary because a function identifier alone is labeled to represent its address and, in the case of a function call, must also include a table of parameters enclosed in parentheses.
You can call a function through a pointer in two ways:
X = (* fptr) ()
X=fptr ()
The second format looks like a function call. But some programmers prefer the first format because it makes it clear that functions are called through pointers rather than function names. Here is an example:
Void (* funcp) ()
Void FileFunc (), EditFunc ()
Main ()
{
Funcp=FileFunc
(* funcp) ()
Funcp=EditFunc
(* funcp) ()
}
Void FileFunc ()
{
Printf (FileFunc\ n)
}
Void EditFunc ()
{
Printf (EditFunc\ n)
}
The program output is:
FileFunc
EditFunc
The main difference is that one is a pointer variable and the other is a function. In use, it is necessary to find out before you can use it correctly.
Second, the pointer of the pointer
The pointer of the pointer looks a little puzzling. Their statements have two asterisks. For example:
Char * * cp
If there are three asterisks, that is the pointer of the pointer, four asterisks are the pointer of the pointer, and so on. When you are familiar with simple examples, you will be able to deal with complex situations. Of course, in the actual program, only two-level pointers are generally used, and three asterisks are not common, let alone four.
The pointer of the pointer needs the address of the pointer.
Char caterpillar A'
Char * pawns
Char * * cp=&p
Through the pointer of the pointer, you can access not only the pointer it points to, but also the data that the pointer it points to. Here are a few examples:
Char * p1=*cp
Char c1=**cp
You may wonder what the use of such a structure is. The pointer of the pointer allows the called function to modify local pointer variables and process the pointer array.
Void FindCredit (int * *)
Main ()
{
Int vals [] = {7, 6, 5, 5, 4, 3, 2, 1, 0]
Int * fp=vals
FindCredit & fp)
Printf (% d\ njinghouse FP)
}
Void FindCredit (int * * fpp)
{
While (* * fppendron 0)
If (* * fpp
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.