In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to use typedef function pointer, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.
Usage one:
Typedef int (* MYFUN) (int, int)
This usage is generally used when defining aliases for functions.
The above example defines that MYFUN is a function pointer, and the function type takes two int arguments and returns an int
The following methods can be used when analyzing this form of definition:
First remove the typedef and aliases, and the rest is the type of the original variable.
After removing typedef and MYFUN, all that's left is:
Int (*) (int, int)
Usage 2:
Typedef defines an alias for the variable type.
Typedef struct {
Int a
Int b
} MY_TYPE
Here you directly give an unnamed structure an alias called MY_TYPE, so you can do this if you want to define an instance of the structure:
MY_TYPE tmp
The second usage: typedef original variable type alias
The use of simple function pointers
/ / form 1: return type (* function name) (parameter table)
Char (* pFun) (int)
/ / typedef char (* pFun) (int) / / the same function as the previous line
/ * the function of typedef is to define new types. The first sentence defines a type of PTRFUN and defines it as a pointer to a function that takes an int as an argument and returns a char type. , /
Char glFun (int a) {return;}
Void main ()
{
PFun = glFun
(* pFun) (2)
}
The first line defines a pointer variable pFun. It is a pointer to a function whose argument is an int type and the return value is a char type. We can't use this pointer only in the first sentence because we haven't assigned it yet.
The second line defines a function glFun (). This function happens to be a function that returns char with int as an argument. We need to understand at the pointer level that the function name of a function is actually a pointer, and the function name points to the first address of the function's code in memory.
Here is an example:
C code collection code
/ / # include
# include
Typedef int (* FP_CALC) (int, int)
/ / Note that this is not a function declaration but a function definition. It is an address. You can output add directly to see it.
Int add (int a, int b)
{
Return a + b
}
Int sub (int a, int b)
{
Return a-b
}
Int mul (int a, int b)
{
Return a * b
}
Int div (int a, int b)
{
Return b? A _ b:-1
}
/ / define a function with an argument of op and return a pointer. The pointer type has two int parameters,
/ / returns a function pointer of type int. Its function is to return the address of the corresponding function according to the operator
FP_CALC calc_func (char op)
{
Switch (op)
{
Case'+': return add;// returns the address of the function
Case'-': return sub
Case'*': return mul
Case'/': return div
Default:
Return NULL
}
Return NULL
}
/ / s_calc_func is a function, and its argument is op
/ / the return value is a function pointer with two int parameters and a return type of int
Int (* s_calc_func (char op)) (int, int)
{
Return calc_func (op)
}
/ / A function called directly by the end user, which receives two int integers and an arithmetic operator, and returns the result of the operation of two numbers
Int calc (int a, int b, char op)
{
FP_CALC fp = calc_func (op); / / get the address of the function for various operations according to the budget character
Int (* s_fp) (int, int) = s_calc_func (op); / / for testing
/ / ASSERT (fp = = s_fp); / / it can be asserted that the two are equal
If (fp) return fp (a, b); / / call the corresponding function according to the address of the function obtained in the previous step and return the result
Else return-1
}
Void main ()
{
Int a = 100, b = 20
Printf ("calc (% d,% d,% c) =% d\ n", a, b,'+', calc (a, b,'+'))
Printf ("calc (% d,% d,% c) =% d\ n", a, b,'-', calc (a, b,'-'))
Printf ("calc (% d,% d,% c) =% d\ n", a, b,'*', calc (a, b,'*'))
Printf ("calc (% d,% d,% c) =% d\ n", a, b,'/', calc (a, b,'/'))
}
Running result
Calc (100,20, +) = 120
Calc (100,20, -) = 80
Calc (100,20, *) = 2000
Calc (100,20, /) = 5
On how to use the typedef function pointer to share here, I hope the above content can be of some help to 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.