//定义这样一个函数
void easyFunc()
{
printf("I'm a easy Function\n");
}
//声明一个函数
void easyFunc();
//调用函数
easyFunc();
//定义这样一个函数
void easyFunc()
{
printf("I'm a easy Function\n");
}
//声明一个函数
void easyFunc();
//调用函数
easyFunc();
int *p = &a;//定义了一个存储整形地址的指针p
void ()
int add(int a, int b);
int (int a, int b)
int (int a, int b) * //这个是绝对错误的
int (*) (int a, int b);//这里的型号在中间,一定要用括号括起来 int (*) (int a, int b);//这里的型号在中间,一定要用括号括起来
int (*p)(int a, int b) = NULL;//初始化为 NULL int (*p)(int a, int b) = NULL;//初始化为 NULL
int add(int a, int b)
{
return a + b;
}
p = add;//给函数指针赋值
int add(int a, int b)
{
return a + b;
}
p = add;//给函数指针赋值
p(5, 6);//等价于 add(5, 6);
printf("%d\n", p(5, b));
p(5, 6);//等价于 add(5, 6);
printf("%d\n", p(5, b));
#include <stdio.h>
typedef int (*Func)(int);
int Double(int a)
{
return (a + a);
}
int main()
{
Func p = Double;
printf("%p\n", p);
return 0;
}
[lqy@localhost notlong]$ gcc -O2 -o func1 func1.c [lqy@localhost notlong]$ ./func1 0x80483d0 [lqy@localhost notlong]$
[lqy@localhost notlong]$ nm func1 | sort 08048294 T _init 08048310 T _start 08048340 t __do_global_dtors_aux 080483a0 t frame_dummy 080483d0 T Double 080483e0 T main ...
void contactMethod(int answer)
{
//把答案输出
printf("答案为:%d\n", answer);
}
void contactMethod(int answer)
{
//把答案输出
printf("答案为:%d\n", answer);
}
小红这边得拿到小明的联系方式,需要用函数指针来存储这个方法:
void tellXiaoMing(int xiaoHongAnswer, void (*p)(int))
{
p(xiaoHongAnswer);
}
//当小红把答案做出来的时候,小红把答案通过小明留下的联系方式传过去
tellXiaoMing(4, contactMethod);
void tellXiaoMing(int xiaoHongAnswer, void (*p)(int))
{
p(xiaoHongAnswer);
}
//当小红把答案做出来的时候,小红把答案通过小明留下的联系方式传过去
tellXiaoMing(4, contactMethod);
//定义个结构体 student,包含name,age 和 score
struct student {
char name[255];
int age;
float score;
};
//typedef struct student 为 Student
typedef struct student Student;
//定义比较结果枚举
enum CompareResult {
Student_Lager = 1, //1 代表大于
Student_Same = 0,// 0 代表等于
Student_Smaller = -1// -1 代表小于
};
//typedef enum CompareResult 为 StudentCompareResult
typedef enum CompareResult StudentCompareResult;
/*
通过成绩来比较学生
*/
StudentCompareResult compareByScore(Student st1, Student st2)
{
if (st1.score > st2.score) {//如果前面学生成绩高于后面学生成绩,返回 1
return Student_Lager;
}
else if (st1.score == st2.score) {//如果前面学生成绩等于后面学生成绩,返回 0
return Student_Same;
}
else { //如果前面学生成绩低于后面学生成绩,返回 -1
return Student_Smaller;
}
}
/*
通过年龄来比较学生
*/
StudentCompareResult compareByAge(Student st1, Student st2)
{
if (st1.age > st2.age) {//如果前面学生年龄大于后面学生年龄,返回 1
return Student_Lager;
}
else if (st1.age == st2.age) {//如果前面学生年龄等于后面学生年龄,返回 0
return Student_Same;
}
else {//如果前面学生年龄小于后面学生年龄,返回 -1
return Student_Smaller;
}
}
/*
通过名字来比较学生
*/
StudentCompareResult compareByName(Student st1, Student st2)
{
if (strcmp(st1.name, st2.name) > 0) {//如果前面学生名字在字典中的排序大于后面学生名字在字典中的排序,返回 1
return Student_Lager;
}
else if (strcmp(st1.name, st2.name) == 0) {//如果前面学生名字在字典中的排序等于后面学生名字在字典中的排序,返回 0
return Student_Same;
}
else {//如果前面学生名字在字典中的排序小于后面学生名字在字典中的排序,返回 -1
return Student_Smaller;
}
}
/*
根据不同的比较方式进行学生排序
stu1[]:学生数组
count :学生个数
p :函数指针,来传递不同的比较方式函数
*/
void sortStudent(Student stu[], int count, StudentCompareResult (*p)(Student st1, Student st2))
{
for (int i = 0; i < count - 1; i++) {
for (int j = 0; j < count - i - 1; j++) {
if (p(stu[j], stu[j + 1]) > 0) {
Student tempStu = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = tempStu;
}
}
}
}
//定义四个学生结构体
Student st1 = {"lingxi", 24, 60.0};
Student st2 = {"blogs", 25, 70.0};
Student st3 = {"hello", 15, 100};
Student st4 = {"world", 45, 40.0};
//定义一个结构体数组,存放上面四个学生
Student sts[4] = {st1, st2, st3, st4};
//输出排序前数组中的学生名字
printf("排序前\n");
for (int i = 0; i < 4; i++) {
printf("name = %s\n", sts[i].name);//输出名字
}
//进行排序
sortStudent(sts, 4, compareByName);
//输出排序后数组中的学生名字
printf("排序后\n");
for (int i = 0; i < 4; i++) {
printf("name = %s\n", sts[i].name);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有