C语言实例:函数指针
函数指针:函数指针数组的使用:
不带参数和返回值的函数指针:
#include <stdio.h>
#include <stdlib.h>
//定义一个没有返回值也没有入口参数的函数指针
typedef void (CONTROL_FLOW_FUNCTION)();
void Function1()
{
printf(" This is printed by function1 !!!\n");
}
void Function2()
{
printf(" 22222222\n");
printf(" 22222222\n");
}
void Function3()
{
printf(" This is printed by function3......\n");
}
CONTROL_FLOW_FUNCTION *ControlFlowFunctions[] = {
Function1,
Function2,
Function3,
NULL
};
char *ControlFlowFunctionNames[] = {
"Function1",
"Function2",
"Function3",
NULL
};
int main()
{
;
printf("main start!\n");
; ControlFlowFunctions[i]!=NULL; i++){
printf("ControlFlow.%s\n", ControlFlowFunctionNames[i]);
ControlFlowFunctions[i]();
}
printf("main end!\n");
}

带参数和返回值的函数指针:
#include <stdio.h>
#include <stdlib.h>
//定义一个函数指针类型
typedef int (CONTROL_FILTER_FUNCTION)(int dat);
int Function1(int data)
{
printf("Check if data can be exactly divisible by 2.\n");
== )
;
else
;
}
int Function2(int data)
{
printf("Check if data can be exactly divisible by 3.\n");
== )
;
else
;
}
int Function3(int data)
{
printf("Check if data can be exactly divisible by 5.\n");
== )
;
else
;
}
CONTROL_FILTER_FUNCTION *ControlFilterFunctions[] = {
Function1,
Function2,
Function3,
NULL
};
char *ControlFilterFunctionNames[] = {
"Function1",
"Function2",
"Function3",
NULL
};
int main()
{
;
;
printf("==Check if data can be exactly divisible by 2 or 3 or 5==.\n");
; ControlFilterFunctions[i]!=NULL; i++){
printf("ControlFilter.%s\n", ControlFilterFunctionNames[i]);
if(ControlFilterFunctions[i](data))
{
printf("YES::%d can be exactly divisible by 2 or 3 or 5.\n", data);
;
}
}
printf("NO::%d can't be exactly divisible by 2 or 3 or 5.\n", data);
;
}



C语言实例:函数指针的更多相关文章
- c语言之函数指针应用
c语言之函数指针应用 1.函数指针与指针函数 在开始运用函数指针前,我们需要将两个概念即:函数指针与指针函数搞清楚. 函数指针,指明这个一个函数,但返回值为指针类型,语法格式为: 类型名* 函数名A( ...
- c语言之函数指针
一.基础研究 这里研究的内容是函数指针,需要我们在研究后构造程序来描述函数指针数组的用法和向函数传函数指针的方法. 指针有很多种:整型指针.结构体指针.数组指针等等,它们的本质是它们的值都是一个地址, ...
- 谈谈自己对C语言中函数指针的一些理解 (第一次写博客,有点小兴奋哈)
1.函数指针声明的格式及简单的使用 (1)格式:(返回值)(*函数指针名)(参数列表) 例如:声明一个无参数无返回值的函数指针(void)(*p)(void). (2)将函数指针指向某个无参数无 ...
- C语言的函数指针数组(好绕啊~看完这篇估计就通关了)
转自https://www.cnblogs.com/chr-wonder/p/5168858.html int *(*p(int))[3] 今天有人问这个是啥?我一看直接就懵逼了…… 下面做一些简单的 ...
- C语言基础:函数指针 分类: iOS学习 c语言基础 2015-06-10 21:55 15人阅读 评论(0) 收藏
函数指针:指向函数的指针变量. 函数名相当于首地址. 函数指针定义:返回值类型 (*函数指针变量名)(参数类型1,参数类型2,....)=初始值 函数指针类型:返回值类型 (*)(参数类型1,参数 ...
- c语言定义函数指针和typedef简写
二种方法来定义函数指针 #include<stdio.h> #include<stdlib.h> #include<Windows.h> int add(int a ...
- C语言之函数指针、回调函数的使用
一.背景 首先看下如下代码,这个定义是放在头文件的,在程序中tCdrvCallbackFkt也定义了另一个变量,而且括号后面还跟定义了几个变量,不理解这个定义. typedef void (PUBLI ...
- c语言的函数指针和函数指针数组的简单demo
今天,简单记录一下,函数指针和函数指针数组的使用,废话不多说,直接贴上代码,里面有详细的注释,方便以后查阅. #include <cstdio> #include <Windows. ...
- c语言的函数指针
简单定义并间接调用 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<time.h> void singas ...
- 可读性很强的C语言的函数指针定义
通常C/C++程序里面要用到大量的指针,其语法非常难以阅读.比如下面的vp指针类型: #include <iostream> using namespace std; typedef vo ...
随机推荐
- LeetCode - 768. Max Chunks To Make Sorted II
This question is the same as "Max Chunks to Make Sorted" except the integers of the given ...
- vscode编辑器自动生成.vue文件
1.选择“文件 -> 首选项 -> 用户代码片段”,此时,会弹出一个搜索框,输入vue 选择vue后,编辑器会自动打开一个名字为vue.json的文件 2.复制以下内容到这个文件中: { ...
- 【CF429E】Points and Segments 欧拉回路
[CF429E]Points and Segments 题意:给你数轴上的n条线段$[l_i,r_i]$,你要给每条线段确定一个权值+1/-1,使得:对于数轴上的任一个点,所有包含它的线段的权值和只能 ...
- brew 源 & pip 源
brew源: https://www.zhihu.com/question/31360766 摘要 1. 替换formula 索引的镜像(即 brew update 时所更新内容) cd " ...
- error_match问题
当nginx的sendfile处于on的情况下,在docker的环境下修改js文件会出现error_match的问题,应该要在nginx的配置中把sendfile设为off
- 数据交互axios的用法
参考: https://www.cnblogs.com/zhouyangla/p/6753673.html
- python中字符串格式化的四种方法
name = "huangemiling" age= 10 address = 'nanjing' print("My name is %s,age is %d,I co ...
- Linux 命令 which whereis locate find
which: 查询某指令的完整路径 $ which [-a] command -a: 将所有在PATH目录中可以找到的指令均列出. 注意:只搜索PATH下的路径. whereis: 只搜索几个特定目录 ...
- 转:WKT、SRID、EPSG概念
原文地址:WKT.SRID.EPSG概念 EPSG: European Petroleum Survey Group (EPSG), http://www.epsg.org/,它成立于1986年,并在 ...
- Qt自定义界面
https://blog.csdn.net/zhangxiaoyu_sy/article/details/78925221