In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares to you is about what is the C language function pointer and its definition and usage. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it with the editor.
You just need to know about the function pointer. You don't use it much, but you must know it.
What is a function pointer?
If a function is defined in the program, the system allocates a storage space for the function code at compile time, and the first address of this storage space is called the address of the function. And the function name represents this address. Since it is an address, we can define a pointer variable to store it. This pointer variable is called a function pointer variable, or function pointer for short.
So how do you define this pointer variable? Although it also points to the same address, the pointer variable to the function is defined differently from the pointer variable we mentioned earlier. For example:
Int (* p) (int,int)
This statement defines a pointer variable p that points to the function. First of all, it is a pointer variable, so there must be a "*", that is, (* p); secondly, the previous int indicates that the pointer variable can point to a function of type int returned; the two int in parentheses indicate that the pointer variable can point to a function that has two parameters and is both int type. So taken together, this statement means that a pointer variable p is defined, which can point to a function that returns an int type and has two integer arguments. The type of p is int (*) (int,int).
So the function pointer is defined as:
Function returns value type (* pointer variable name) (function argument list)
Function return value Type indicates that the pointer variable can point to a function with what return value type; function argument list indicates that the pointer variable can point to a function with what argument list. Only the parameter type of the function needs to be written in this parameter list.
We can see that the definition of a function pointer is to change the "function name" in the "function declaration" to "(* pointer variable name)". But it should be noted here that the parentheses at both ends of "(* pointer variable name)" cannot be omitted, and the parentheses change the priority of the operator. If parentheses are omitted, instead of defining a function pointer, a function declaration is declared, that is, a function whose return type is pointer.
So how to tell whether a pointer variable is a pointer variable to a variable or a pointer variable to a function? First of all, look at whether there is a "*" in front of the variable name, if there is a "*" indicating that it is a pointer variable; secondly, to see if there are parentheses with formal parameter types behind the variable name, and if so, it is a pointer variable pointing to a function, that is, a function pointer, and if not, a pointer variable to a variable.
Finally, it is important to note that pointer variables to functions do not have + + and-- operations.
How to call a function with a function pointer
Let me give you an example:
IntFunc (intx); / * declare a function * /
Int (* p) (intx); / * define a function pointer * /
Paired function * assigns the first address of the Func function to the pointer variable pause /
The function Func has no parentheses or arguments when assigned. Because the function name Func represents the first address of the function, once assigned, the pointer variable p points to the first address of the function Func () code.
The above is what is the C language function pointer and its definition and usage, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.