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;
}
#函数指针
运行时确定调用哪个函数。
#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;
}
相关文章推荐
- Lua编程示例(二):面向对象、metatable对表进行扩展
- C#中面向对象编程机制之多态学习笔记
- 浅谈Lua的面向对象特性
- Lua面向对象之类和继承浅析
- JavaScript面向对象的两种书写方法以及差别
- 浅谈c# 面向对象之类与对象
- C#面向对象特征的具体实现及作用详解
- C# 面向对象的基本原则
- 浅谈对c# 面向对象的理解
- Ruby面向对象编程详解
- php学习 面向对象 课件第1/2页
- PHP程序61条面向对象分析设计的经验小结
- 收集学习asp.net比较完整的面向对象开发流程
- javascript 面向对象编程 万物皆对象
- 不错的JavaScript面向对象的简单入门介绍第1/2页
- [推荐]javascript 面向对象技术基础教程
- javascript 面向对象的JavaScript类
- JavaScript中的面向对象介绍
- javascript实现面向对象类的功能书写技巧
- Javascript 面向对象 继承