我想知道这个 C 函数到底是什么意思?
typedef uint32_t (*test) ( void );
答案 0 :(得分:4)
它基本上读作:
test -- test is
typedef test -- a typedef name (alias) for the type
typedef *test -- pointer to
typedef (*test) ( ) -- a function taking
typedef (*test) ( void ) -- no parameters
typedef uint32_t (*test) ( void ); -- returning uint32_t
所以 test
是类型“指向不带参数并返回 uint32_t
(uint32_t (*)(void)
) 的函数的指针”的别名。
所以如果你有一个像
这样的函数uint32_t foo(void) { return some_value; }
然后您可以使用任一方法创建指向该函数的指针
uint32_t (*ptr)(void) = foo;
或
test ptr = foo;
就风格而言,在 typedef
1 中隐藏类型的指针通常是个坏主意;最好创建一个像
typedef uint32_t test(void); // test is a typedef name for "function taking no parameters
// and returning uint32_t
然后将指针创建为
test *ptr;
答案 1 :(得分:3)
它是一个函数指针的 typdef,指向一个不带参数并返回一个 uint32 的函数。所以不是实际的函数指针,而是该类型的别名。
如何使用它的示例:
#include <stdio.h>
#include <stdint.h>
typedef uint32_t (*test) ( void );
uint32_t my_test1(void)
{
return 100;
}
// Output of this program will be: 100
int main (void)
{
test func = my_test1;
printf("%d\n", func());
}