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 ...
随机推荐
- ASP.NET MVC自定义视图引擎ViewEngine 创建Model的专属视图
MVC内置的视图引擎有WebForm view engine和Razor view engine,当然也可以自定义视图引擎ViewEngine. 本文想针对某个Model,自定义该Model的专属视图 ...
- PHP 使用 GeoLiteCity 库解析 IP 为地理位置
关于把 IP 地址转换为地理位置可以使用网络上很多的 API,好处就是不用在本地存储一个 IP 数据库,而且一般网络上的 IP 库会自动更新,不利的地方就是太依赖于网络,性能表现也可能会弱些.比如像下 ...
- 4Sum_leetCode
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- 使用GitBook, Git github, Markdown 快速发布你的书籍
利用git作为版本管理和发布工具 你可以直接通过npm安装gitbook到全局 npm install -g gitbook gitbook只提供了如下四个命令 gitbook -h Usage: g ...
- hihoCoder #1388 : Periodic Signal
NTT (long long 版) #include <algorithm> #include <cstring> #include <string.h> #inc ...
- CAShapeLayer实现圆形进度条效果
一.CAShapeLayer简单介绍: .CAShapeLayer继承至CALayer,能够使用CALayer的全部属性值 2.CAShapeLayer须要与贝塞尔曲线配合使用才有意义 3.使用CAS ...
- C语言合并两个集合(L,L1) 将L1中不在L中的元素插入到L线性表中
void main(){ Sqlist L,L1; InitList(&L); InitList(&L1); ListInsert(&L, 1, 2); ListInsert( ...
- php在不同平台下路径分隔符不同的解决办法
在看phpamf时看到一个常量“DIRECTORY_SEPARATOR”,最后发现是一个全局的常量,用来定义路径分隔符的 主要解决在windows和linux下路径分隔符不同的造成代码不通用的问题,在 ...
- JavaScript点击事件-一个按钮触发另一个按钮
<input type="button" value="Click" id="C" onclick="Go();" ...
- scikit-learn(project中用的相对较多的模型介绍):1.14. Semi-Supervised
參考:http://scikit-learn.org/stable/modules/label_propagation.html The semi-supervised estimators insk ...