1
拆解复杂函数指针
1//signal
2void (*signal(int signum,void(*handler)(int))))(int);
1#include
2#include
3
4typedef int (*tyFuc1)(int ,int );
5typedef int (tyFuc2)(int ,int );
6
7int sAdd(int a,int b)
8{
9 return(a + b);
10}
11
12/**********************************
13 * Function:main
14 * Description:函数指针的两种形式
15 * Author: bug菌
16 ********************************/
17int main(int argc, char *argv[]) {
18
19 tyFuc1 pFuc1 = sAdd;
20 tyFuc2 * pFuc2 = sAdd;
21
22 printf("sum1 = %d\n",pFuc1(1,1));
23 printf("sum2 = %d\n",pFuc2(2,2));
24
25 return 0;
26}
1void ( *signal(int signum,void(*handler)(int)) )(int)
2
3--> pFuc1 *signal(int signum,void(*handler)(int))
4
5--> pFuc1 *signal(int signum,void( *handler )(int))
6
7--> pFuc1 *signal(int signum,pFuc2 *handler )
2
之前案例分析
1 ((void (*)(void*))fuc)( param );
2
3-->( (pFuc * ) fuc)( param );
3
函数指针使用细节
参考实例:
1#include
2#include
3
4typedef int (*tyFuc1)(int ,int );
5typedef int (tyFuc2)(int ,int );
6
7int sAdd(int a,int b)
8{
9 return(a + b);
10}
11
12/**********************************
13 * Function:main
14 * Description:函数指针的两种形式
15 * Author: bug菌
16 ********************************/
17int main(int argc, char *argv[]) {
18
19 tyFuc1 pFuc1 = sAdd;
20 tyFuc2 * pFuc2 = &sAdd;
21
22 // sAdd 与 &sAdd 的区别
23 printf(" sAdd: = 0x%x\n",sAdd);
24 printf("&sAdd: = 0x%x\n",&sAdd);
25
26 //pFuc1与 ***pFuc1 的区别
27 printf("sum1 = %d\n",pFuc1(1,1));
28 printf("sum2 = %d\n",(*pFuc2)(2,2));
29 printf("sum3 = %d\n",(***pFuc2)(2,2));
30
31 return 0;
32}
运行结果:
1 0x004016bd <+0>: push ebp
2 0x004016be <+1>: mov ebp,esp
3 0x004016c0 <+3>: and esp,0xfffffff0
4 0x004016c3 <+6>: sub esp,0x20
5 0x004016c6 <+9>: call 0x401d00 <__main>
6 0x004016cb <+14>: mov DWORD PTR [esp+0x1c],0x4016b0
7 0x004016d3 <+22>: mov DWORD PTR [esp+0x18],0x4016b0
8 0x004016db <+30>: mov DWORD PTR [esp+0x4],0x4016b0
9 0x004016e3 <+38>: mov DWORD PTR [esp],0x405064
10 0x004016ea <+45>: call 0x403708
11 0x004016ef <+50>: mov DWORD PTR [esp+0x4],0x4016b0
12 0x004016f7 <+58>: mov DWORD PTR [esp],0x405074
13 0x004016fe <+65>: call 0x403708
14 0x00401703 <+70>: mov DWORD PTR [esp+0x4],0x1
15 0x0040170b <+78>: mov DWORD PTR [esp],0x1
16 0x00401712 <+85>: mov eax,DWORD PTR [esp+0x1c]
17 0x00401716 <+89>: call eax
18 0x00401718 <+91>: mov DWORD PTR [esp+0x4],eax
19 0x0040171c <+95>: mov DWORD PTR [esp],0x405084
20 0x00401723 <+102>: call 0x403708
21 0x00401728 <+107>: mov DWORD PTR [esp+0x4],0x2
22 0x00401730 <+115>: mov DWORD PTR [esp],0x2
23 0x00401737 <+122>: mov eax,DWORD PTR [esp+0x18]
24 0x0040173b <+126>: call eax
25 0x0040173d <+128>: mov DWORD PTR [esp+0x4],eax
26 0x00401741 <+132>: mov DWORD PTR [esp],0x40508f
27 0x00401748 <+139>: call 0x403708
28 0x0040174d <+144>: mov DWORD PTR [esp+0x4],0x2
29 0x00401755 <+152>: mov DWORD PTR [esp],0x2
30 0x0040175c <+159>: mov eax,DWORD PTR [esp+0x18]
31 0x00401760 <+163>: call eax
32 0x00401762 <+165>: mov DWORD PTR [esp+0x4],eax
33 0x00401766 <+169>: mov DWORD PTR [esp],0x40509a
34 0x0040176d <+176>: call 0x403708
35 0x00401772 <+181>: mov eax,0x0
36 0x00401777 <+186>: leave
37 0x00401778 <+187>: ret
最后
END