include<stdio.h>
typedef int(FUNC)(int);
int test(int i){
return i*i;
}
void f()
{
printf("Call f()...\n");
}
int main(){
FUNC* pt=test;
void (* pf)()=&f; //也可以直接写函数名
printf("pf=%p\n",pf);
printf("f=%p\n",f);
printf("&f=%p\n",&f);
pf();
(*pf)();
printf("Function pointer call:%d\n",pf(2));
return 0;
}
--------------------------------------
回调函数的( 函数指针的使用)
---------------------------------------
#include<stdio.h>
typedef int(*Weapon)(int);
void fight(Weapon wp,int arg){
int result=0;
printf("Fight boss!\n");
result=wp(arg);
printf("Boss loss:%d\n",result);
}
int knife(int n){
int ret=0;
int i=0;
for(i:n){
printf("Knife attack:%d\n",1)
ret++;
}
return ret;
}
int gun(int n){
int ret=0;
int i=0;
for(i:n){
printf("Gun attack:%d\n",10);
ret+=10;
}
return ret;
}
int main(){
fight(knife,3);
fight(gun,5);
return 0;
}
typedef int(FUNC)(int);
int test(int i){
return i*i;
}
void f()
{
printf("Call f()...\n");
}
int main(){
FUNC* pt=test;
void (* pf)()=&f; //也可以直接写函数名
printf("pf=%p\n",pf);
printf("f=%p\n",f);
printf("&f=%p\n",&f);
pf();
(*pf)();
printf("Function pointer call:%d\n",pf(2));
return 0;
}
--------------------------------------
回调函数的( 函数指针的使用)
---------------------------------------
#include<stdio.h>
typedef int(*Weapon)(int);
void fight(Weapon wp,int arg){
int result=0;
printf("Fight boss!\n");
result=wp(arg);
printf("Boss loss:%d\n",result);
}
int knife(int n){
int ret=0;
int i=0;
for(i:n){
printf("Knife attack:%d\n",1)
ret++;
}
return ret;
}
int gun(int n){
int ret=0;
int i=0;
for(i:n){
printf("Gun attack:%d\n",10);
ret+=10;
}
return ret;
}
int main(){
fight(knife,3);
fight(gun,5);
return 0;
}