参考:http://light4.github.com/Linux-C/ch23s08.html
/*
* =====================================================================================
*
* Filename: b.c
*
* Description:
*
* Version: 1.0
* Created: 09/15/2012 17:04:03
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef void F(const char*); //define function type F
typedef void (*PF)(const char*); //define a function pointer type PF
F a;
F* retPF(F* f){ // a function return a "function pointer type"
(*f)("in Ret");
return f;
}
int
main ( int argc, char *argv[] )
{
a("hell");
PF pF = a;
printf ("\nProgram %s\n\n", argv[0] );
(*pF)("hello");
F* rP = retPF(a);
rP("rP printed");
PF pF2 = retPF(a);
pF2("PF2 printed");
return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */
void a(const char* str){
printf("%s\n", str);
}
本文通过一个具体的C语言程序实例介绍了如何定义和使用函数指针,包括函数指针类型的声明、函数指针变量的定义及赋值,以及如何通过函数指针调用函数。此外还展示了如何创建返回函数指针的函数。
1万+

被折叠的 条评论
为什么被折叠?



