typedef的用法是在定义前加上这个关键字,就能得到一个新的类型名;例如int a;变成typedef int a ;那么就可以用a表示Int了。
比如void (*test)(void);那么加上typedef就变成了typedef void (*test)(void);
类型名test就表示一个返回值是void参数是void的一个函数指针类型,与上面的int a不同这里的变量名是test
c
void test_opr(void) {};
typedef void (*test)(void);
test ptr = test_opr;
ptr();
void test(void);这个函数声明表示定义了一个参数为void,返回空的test函数。
并且这里面的test的值就是存储函数指针,也就是这个函数的第一条语句的内存地址。
*为解引用操作,对函数进行解引用操作, *test按道理讲要返回函数内容也就是函数体,但是事实上没法返回函数体。就对解引用降级(退化),所以test和*test以及**test不管多少个解引用,结果都是这个函数的内存地址。但还是注意test和*test本质上是不一样的,一个是函数指针存储函数的第一条汇编指令的内存地址,一个是这个函数的函数体,在输出的时候会隐式退化成test。
c
#include <stdio.h>
static int count = 0;
void test(void) {
printf("test count %d task start\n", count++);
}
void (*test_por)(void) = test;
int main() {
printf("test value 0x%X, *test value 0x%X **test value 0x%X &test value 0x%X\n", test, *test, **test, &test);
test();
(*test)();
(**test)();
(*test_por)();
(&test)();
//(&test_por)(); //注意此时这个test_por是存储函数内存地址的容器,也就是说&test_por获取到的是存储函数内存地址的地址所以可以看到后面输出的&test_por地址和test_por不同。也就是说,test_por存储的内存数据,&test_por是存储这个数据的内存地址
//事实上,test_por存储的是函数体的第一条指令的地址
printf("test_por value 0x%X, *test_por value 0x%X **test_por value 0x%X &test_por value 0x%X\n", test_por, *test_por, **test_por, &test_por);
while(1) {
}
return 0;
}
输出的结果也能证实:
c
test value 0x681F14A4, *test value 0x681F14A4 **test value 0x681F14A4 &test value 0x681F14A4
test count 0 task start
test count 1 task start
test count 2 task start
test count 3 task start
test count 4 task start
test_por value 0x681F14A4, *test_por value 0x681F14A4 **test_por value 0x681F14A4 &test_por value 0x681F9000