Function Pointers in C
来源:https://cs.nyu.edu/courses/spring12/CSCI-GA.3033-014/Assignment1/function_pointers.html
Function Pointers in C
Just as a variable can be declared to be a pointer to an int, a variable can also declared to be a pointer to a function (or procedure). For example, the following declares a variable v whose type is a pointer to a function that takes an int as a parameter and returns an int as a result:
int (*v)(int); That is, v is not itself a function, but rather is a variable that can point to a function.
Since v does not yet actually point to anything, it needs to be assigned a value (i.e. the address of a function). Suppose you have already defined the following function f, as follows.
int f(int x) { return x+1; }
To make v point to the function f, you simply need to assign v as follows:
v = f; To call the function that v points to (in this case f), you could write, for example,
... (*v)(6) ...
That is, it would look like an ordinary function call, except that you would need to dereference v using * and wrap parentheses around the *v.
Putting it all together, here is a simple program:
#include int (*v)(int); int f(int x) { return x+1; } main() { v = f; printf("%d\n", (*v)(3)); } Notice that it is often convenient to use a typedef to define the function type. For example, if you write
typedef void (*MYFNPOINT)(int);
then you have defined a type MYFNPOINT. Variables of this type point to procedures that take an int as a parameter and don't return anything. For example,
MYFNPOINT w;
declares such a variable.
Of course, you can declare a struct (record) type the contains function pointers, among other things. For example,
typedef struct { int x; int (*f)(int, float); MYFNPOINT g; } THING; declares a type THING which is a structure containing an int x and two function pointers, f and g (assuming the definition of the MYFNPOINT type, above).
Function Pointers in C的更多相关文章
- C# unmanaged function pointers for iOS
C# unmanaged function pointers for iOS Just a reminder to myself when I need this thing next time fo ...
- new thoughts over function pointers
Previous works do not relate to function pointers, but reading some documents reading and learning S ...
- tips~function pointer
An simple example: #include<stdio.h> int plus(int a,int b) { return a+b; } int main() { int (* ...
- Linux Programe/Dynamic Shared Library Entry/Exit Point && Glibc Entry Point/Function
目录 . 引言 . C/C++运行库 . 静态Glibc && 可执行文件 入口/终止函数 . 动态Glibc && 可执行文件 入口/终止函数 . 静态Glibc & ...
- Effective Java 21 Use function objects to represent strategies
Theory In the Java the function pointers is implemented by the declaring an interface to represent s ...
- std::function,std::bind
std::function 和 std::bind 标准库函数bind()和function()定义于头文件中(该头文件还包括许多其他函数对象),用于处理函数及函数参数.bind()接受一个函数(或者 ...
- (C/C++) Callback Function 回调(diao)函数
原文: http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557/Callback-Functions-Tutorial ...
- (转) Pointers
原地址 http://www.cplusplus.com/doc/tutorial/pointers/ Pointers In earlier chapters, variables have bee ...
- c++ virturn function -- 虚函数
c++ virturn function -- 虚函数 pure irtual function -- 纯虚函数 先看例子 #include <iostream> using nam ...
随机推荐
- TCP粘包处理通用框架--C代码
说明:该文紧接上篇博文“ linux epoll机制对TCP 客户端和服务端的监听C代码通用框架实现 ”讲来 (1)TCP粘包处理数据结构设计 #define MAX_MSG_LEN 65535 ty ...
- llinux获取系统时间
linux中获取当前时间.统计程序运行时间,可以使用gettimeofday()得到毫秒级的时间统计,利用rdtsc指令获取纳秒级时间统计. gettimeofday() 它是一个linux C库函数 ...
- PropertyUtils复制BigDecimal异常
PropertyUtils复制BigDecimal会引发异常,要注意
- Ubuntu配置apache2.4配置虚拟主机遇到的问题
update: 偶然看到了 apache的更新说明,直接贴个地址过来吧. http://httpd.apache.org/docs/2.4/upgrading.html 最近想把web开发目录从/va ...
- Prometheus+Grafana搭建监控系统
之前在业务中遇到服务器负载过高问题,由于没有监控,一直没发现,直到业务方反馈网站打开速度慢,才发现问题.这样显得开发很被动.所以是时候搭建一套监控系统了. 由于是业余时间自己捯饬,所以神马业务层面的监 ...
- web应用的负载均衡、集群、高可用(HA)解决方案
看看别人的文章: 1.熟悉几个组件 1.1.apache —— 它是Apache软件基金会的一个开放源代码的跨平台的网页服务器,属于老牌的web服务器了,支持基于Ip或者域名的虚拟主机,支持代 ...
- 国家制定人工智能(AI)发展战略的决策根据
在今年两会上,李彦宏的提案有何道理?提案的依据是什么?这个问题必须说清楚,对社会公众有个交代. 回想过去,早在上世纪九十年代,用"电子网络"模拟人脑的想法已经出现.这样的" ...
- TSharding源码阅读
需要的背景知识:Spring 和Mybatis 实现原理和源码, javaassist字节码增强的使用, java及设计模式的使用 1 读取解析数据库配置文件 DataSourceScanner实现了 ...
- 自学宝典:10个学习Android开发的网站推荐
1. Android Developers 作为一个Android开发者,官网的资料当然不可错过,从设计,培训,指南,文档,都不应该错过,在以后的学习过程中慢慢理解体会. 2. Android Gui ...
- poj2485
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 27912 Accepted: 12734 Descri ...