来源: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的更多相关文章

  1. 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 ...

  2. new thoughts over function pointers

    Previous works do not relate to function pointers, but reading some documents reading and learning S ...

  3. tips~function pointer

    An simple example: #include<stdio.h> int plus(int a,int b) { return a+b; } int main() { int (* ...

  4. Linux Programe/Dynamic Shared Library Entry/Exit Point && Glibc Entry Point/Function

    目录 . 引言 . C/C++运行库 . 静态Glibc && 可执行文件 入口/终止函数 . 动态Glibc && 可执行文件 入口/终止函数 . 静态Glibc & ...

  5. 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 ...

  6. std::function,std::bind

    std::function 和 std::bind 标准库函数bind()和function()定义于头文件中(该头文件还包括许多其他函数对象),用于处理函数及函数参数.bind()接受一个函数(或者 ...

  7. (C/C++) Callback Function 回调(diao)函数

    原文: http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557/Callback-Functions-Tutorial ...

  8. (转) Pointers

    原地址 http://www.cplusplus.com/doc/tutorial/pointers/ Pointers In earlier chapters, variables have bee ...

  9. c++ virturn function -- 虚函数

    c++ virturn function -- 虚函数 pure irtual function  -- 纯虚函数   先看例子 #include <iostream> using nam ...

随机推荐

  1. 机器学习2—K近邻算法学习笔记

    Python3.6.3下修改代码中def classify0(inX,dataSet,labels,k)函数的classCount.iteritems()为classCount.items(),另外p ...

  2. 编程算法 - 二叉树的最低公共祖先 代码(C)

    二叉树的最低公共祖先 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 二叉树的最低公共祖先(lowest common ancestor), 首先先序遍 ...

  3. mongodb 指南

    mongodb概述 mongodb安装 mongodb启动参数 mongodb权限体系 mongodb管理命令 mongodb备份恢复

  4. volley全然解析

    一.volley是什么? 1.简单介绍   Volley是Goole在2013年Google I/O大会上推出了一个新的网络通信框架,它是开源的.从名字由来和配图中无数急促的火箭能够看出 Volley ...

  5. 对象复制帮助类---DeepCopy

    有的时候我们在对一个引用类型的对象进行传递操作的时候希望不要直接修改传递过来的对象,而是复制出一份来操作的时候就可以用下面的类进行复制 sing System.IO; using System.Run ...

  6. PHP-Manual的学习----【语言参考】----【类型】-----【Interger类型】

    2017年7月20日15:48:46Integer 整型 1.什么是整数?正数 0 负数2.整型值可以使用十进制,十六进制,八进制或二进制表示,前面可以加上可选的符号(- 或者 +). 二进制表达的 ...

  7. Android创建library工程

    本文着重介绍如何创建Android library,并且在工程中使用此library提供的资源,具体步骤如下:1. 创建一个Android工程,命名为sourceProj2. 右键--properti ...

  8. js实现table内 某列的内容进行即时筛选

    往往有些时候,我们把数据从数据库读取出来,显示到table里面,而此时来了个新需求,要在一个搜索框内输入关键字,表格的内容进行即时的筛选. 而即时触发进行数据库的查询,再回调显示,就显得慢,拖累服务器 ...

  9. css 坑记

    1. div 内容超出 (做换行处理)   要注意 white-space属性的运用 设置 div width:100%;(或者固定值) 设置换行  word-break: break-all; 设置 ...

  10. java中枚举类型的使用

    Java 枚举(enum) 详解7种常见的用法 JDK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便. web项目里实体类使用枚举类型: 一般在该实体 ...