您的位置:首页 > 其它

C函数指针

2015-10-12 00:00 134 查看
摘要: 简单介绍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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息