#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();
}
函数指针 typedef 用法 回调函数 结构体存储函数地址
最新推荐文章于 2023-05-06 00:32:27 发布