函数指针 typedef 用法 回调函数 结构体存储函数地址

#include <stdio.h>
#include<stdlib.h>
//01定义简单函数
void f1(int a)
{
	printf("hello\n");
}
void f2(int a)
{
	printf("world\n");
}

//02  typedef 用法
typedef int  myint;
//给已有的类型   起  别名    方便使用

typedef void(*type)(int);

//03--函数指针作为形参--------回调函数
void func(type p, int a)
{
	p(a);
}

//04 结构体存储函数地址,实现面向对象的思想
struct stu
{
	int age;
	int(*speak)(int);
};
int shuo(int a)
{
	printf("hello world");
	return 0;
}
void test1()
{
	//函数指针变量定义
	void(*p)(int) = NULL;
	p = f1;
	p(1);
	p = f2;
	p(2);

}
void test2()
{
	//函数指针变量定义
	type p = NULL;
	p = f1;
	p(1);
	p = f2;
	p(2);

}
void test3()
{
	int a = 0;
	func(f1, a);
	func(f2, a);//回调函数
}
void test4()
{
	struct stu s;
	s.age = 100;
	s.speak = shuo;
	printf("age:%d----speak:%d", s.age, s.speak(1));
}
void main()
{
	//test1();
	//test2();
	//test3();
	test4();



	getchar();
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值