来源: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. Python读取文件文件夹并检索

    import os import os.path f=open("Shouldlist.txt") ShouldList=[] while 1: line =f.readline( ...

  2. lua table库

      整理自:http://www.cnblogs.com/whiteyun/archive/2009/08/10/1543139.html 1.table.concat(table, sep,  st ...

  3. Arbitrage - poj 2240 (Bellman-ford)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17374   Accepted: 7312 Description Ar ...

  4. 【Mac系统 + Python + Django】之搭建第一个【Django Demo(一)】

    我编写的此系列学习资料是通过虫师的python接口自动化出的书学习而来的,在此说明一下,想学习更多的自动化的同学可以找虫师的博客园,非广告,因为我python+selenium自动化也是跟虫师学的,学 ...

  5. 【Mac系统 + Git】之上传项目代码到github上以及删除某个文件夹

    之前做开发的时候,用过一段时间git代码管理工具,用命令行操作感觉十分高大上,今天我想从头总结一篇Mac系统下如何利用git上传代码到github上的学习. 目录 一.安装Git 二.创建.ssh文件 ...

  6. php_screw加密安装

    php_screw的安装与使用 1.下载:http://sourceforge.net/projects/php-screw/files/ php文件通常以文本格式存贮在服务器端, 很容易被别人读到源 ...

  7. Xcode8:"subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0" 的警告

    运行xcode8遇到这个警告: subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0, persist_lev ...

  8. C语言基础知识【函数】

    C 函数1.函数是一组一起执行一个任务的语句.每个 C 程序都至少有一个函数,即主函数 main() ,所有简单的程序都可以定义其他额外的函数.您可以把代码划分到不同的函数中.如何划分代码到不同的函数 ...

  9. python mysql orm

    Python中操作mysql的pymysql模块详解:https://www.cnblogs.com/wt11/p/6141225.html Python 12 - Mysql & ORM:h ...

  10. Hadoop伪分布式环境快速搭建

    Hadoop分支 Apache Cloudera Hortonworks 本文是采用Cloudera分支的hadoop. 下载cdh-5.3.6 版本 下载地址:http://archive.clou ...