C函數指針

#函數指針 運行時肯定調用哪一個函數。 #include <stdio.h> void hello(void){ printf("Hello\n"); } void world(void){ printf("World\n"); } typedef int I; typedef void (*Func)(void); int main(void){ I i; scanf("%d",&i); void (*func0)(void); Func func1; if(i==0){ func0=hello; func1=hello; }else{ func0=world; func1=world; } func0(); func1(); return 0; } #形參函數指針 #include <stdio.h> void ArrForEach(int *arr,int len,void(func)(int)){ int i; for(i=0;i<len;++i){ func(&arr[i]); } } void PrintInt(int *i){ printf("%d,",*i); } void IncInt(int *i){ ++*i; } void DecInt(int *i){ --*i; } int main(void){ int arr[]={1,2,3,4,5,6,7,8,9,0}; ArrForEach(arr,10,PrintInt); printf("\n"); ArrForEach(arr,10,IncInt); ArrForEach(arr,10,PrintInt); printf("\n"); ArrForEach(arr,10,DecInt); ArrForEach(arr,10,PrintInt); printf("\n"); return 0; } 函數指針實現排序 #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct AB{ int a; char b[16]; }AB; int ABCmp(const void *ab0Arg,const void *ab1Arg){ AB ab0=(AB)ab0Arg; AB ab1=(AB)ab1Arg; int result=ab0->a-ab1->a; if(result!=0) return result; return strcmp(ab0->b,ab1->b); } int main(void){ AB ab[]={ {1,"ab"}, {9,"df"}, {8,"ab"}, {1,"ab"}, {2,"ab"}, {2,"cd"}, }; int len=sizeof(ab)/sizeof(ab[0]); qsort(ab,len,sizeof(ab[0]),ABCmp); int i; for(i=0;i<len;++i){ printf("{%d,%s},",ab[i].a,ab[i].b); } printf("\n"); return 0; } #回調函數 #include <stdio.h> void Func(const char *str){ printf("數據:%s",str); } void DataMain(void (*func)(const char *str)){ for(;;){ char str[BUFSIZ]; fgets(str,sizeof(str),stdin); func(str); } } int main(void){ DataMain(Func); return 0; } #面向對象 #include <stdio.h> typedef struct Dog{ int age; void (*Sound)(struct Dog *dog); }Dog; void DogSound(Dog *dog){ printf("年齡:%d,汪汪。\n",dog->age); } void DogInit(Dog *dog,int age){ dog->age=age; dog->Sound=DogSound; } int main(void){ Dog dog; DogInit(&dog,10); dog.Sound(&dog); return 0; } #面向接口 Animal.h #ifndef ANIMAL_H #define ANIMAL_H typedef struct Animal{ void (*Run)(struct Animal *animal); void (*Sound)(struct Animal *animal); void (*Destroy)(struct Animal *animal); }Animal; Animal *NewDog(); Animal *NewCat(); #endif Animal.c #include "Animal.h" #include <stdio.h> #include <stdlib.h> typedef struct Dog{ Animal animal; }Dog; void DogRun(Animal *animal){ printf("跑的快\n"); }
void DogSound(Animal *animal){ printf("汪汪\n"); }
void DogDestroy(Animal *animal){ free(animal); }
Animal *NewDog(){ Dog *dog=malloc(sizeof(Dog)); dog->animal.Run=DogRun; dog->animal.Sound=DogSound; dog->animal.Destroy=DogDestroy; return &dog->animal; }
typedef struct Cat{ Animal animal; }Cat; void CatRun(Animal *animal){ printf("跑的慢,會爬樹\n"); } void CatSound(Animal *animal){ printf("喵喵\n"); } void CatDestroy(Animal *animal){ free(animal); } Animal *NewCat(){ Cat *dog=malloc(sizeof(Cat)); dog->animal.Run=CatRun; dog->animal.Sound=CatSound; dog->animal.Destroy=CatDestroy; return &dog->animal; } Main.c #include "Animal.h" #include <stdio.h> #include <string.h> int main(void){ Animal *animal; char str[BUFSIZ]; fgets(str,sizeof(str),stdin); int len=strlen(str); if(len>0)str[len-1]=0; if(strcasecmp(str,"dog")==0){ animal=NewDog(); }else if(strcasecmp(str,"cat")==0){ animal=NewCat(); }else{ return -1; }
animal->Run(animal); animal->Sound(animal); animal->Destroy(animal); return 0; }函數

相關文章
相關標籤/搜索