中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > C/C++
转一篇关于函数指针的文章
作者:未知 时间:2005-09-13 23:28 出处:Blog.ChinaUnix.net 责编:chinaitpower
              摘要:转一篇关于函数指针的文章
不怕您见笑,以前学c语言,一直都没搞清楚过函数指针的问题,直到我发现了这篇文章:)

不怕您见笑,以前学c语言,一直都没搞清楚过函数指针的问题,直到我发现了这篇文章:)

作者:Peter

出处:http://www.dirac.org/linux/programming/tutorials/function_pointers/


Function Pointers

In this tutorial, the term object (which technically means a region of memory that can be examined and stored into) will be used to mean some kind of C variable. I'll use the terms object and lvalue interchangeably. This is opposed to things like a function or an "rvalue".

What Are Function Pointers?

A C program variable resides at some address within the program's memory space. Exactly where it resides depends on the variable type (automatic, static, global, etc), but the point is, all variables reside somewhere. We can print their address easily. The code to the right illustrates printing the address of an object.

The output of example code 1.c* looks something like:

p@satan$ ./1
i resides at 0xbffff8e4

The "address of" operator, &, operating on i produces address of i and the %p format specifier tells printf() to print a memory address.

As example code 2.c** shows, you can also print the address of a function as well as variables. The output of this code will look like:

p@satan$ ./2
i resides at 0xbffff8e4
function() resides at 0x8048474
main() resides at 0x804841c

Surprised? Don't be. Functions also reside within a program's memory space. By using the "address of" operator, we told the program to do in 2.c exactly what we asked it to do in 1.c. The only difference is that we requested the address of a function rather than a variable.

How To Declare A Pointer To A Function

All variables in C need to be declared and defined. Function pointers are no exception. The first step in using a function pointer is to declare it, and that's what this section is about.

At first thought, you might want to declare a function pointer the same way you'd declare an object pointer. This is almost correct, but not quite.

(incorrect)
Pointer to object
declaration:int i;
definition:int *int_ptr = &i;

Pointer to function
declaration:int f(int arg);
definition:int *func_ptr(int arg) = &f;

Therefore, as with all order of operation problems, we use parenthesis to explicitly define what we want

(correct)
Pointer to object
declaration:int i;
definition:int *int_ptr = &i;

Pointer to function
declaration:int f(int arg);
definition:int (*func_ptr)(int arg) = &f;

It's important that your function pointer and the function it points to are compatible. In other words. Here are some illustrations.

e.g.(1)
extern long double myfunc1(int arg);
long double (*func_ptr)(void) = myfunc1;

This first example is wrong. *func_ptr can only point to functions which take no arguments. However, myfunc1() takes an int argument.

e.g.(2)
extern double myfunc2(char *str);
char (*func_ptr)(char *str) = myfunc2;

This next example is also wrong. *func_ptr can only point to functions which return a char. However, myfunc2() returns a double.

e.g.(3)
extern char *myfunc3(int *arg);
char *(*func_ptr)(int *arg) = myfunc3;

This last example is correct. *func_ptr points to a function with the same return value and arguments that myfunc3() does.

So now you know how to declare function pointers. The next step is to discuss how to make them point to something. In other words, we'll talk about getting address of a function.

How To Get The Address Of A Function

I spilled most of the beans in the previous section, but there are really two ways to get the address of a function. Let funcptr be a function pointer. Suppose we wanted this object to point to a compatible function named myfunc().

The first method is with an implicit conversion to pointer:
funcptr = f;

The second method is with an explicit manufacture of a pointer:
funcptr = &f;

Both ways are completely equivalent (AFAIK) and completely legal. You might think that the explicit method is more consistent because it's analogous to how we get the address of objects. However, the first method is shorter and cleaner. It's up to you.

Calling A Function Using A Function Pointer

Like getting the address of a function, there are two ways to call a function using a pointer to that function.

The first method is an explicit dereference of the pointer, similar to what we use for object pointers:
extern void swap(int x, int y);
void (*func_ptr)(int x, int y) = f;/*declaration and initialization */

(*funcptr)(3, 2);/* call */

The second method is an implicit dereference of the pointer. It's less like what you're used to for object pointers, but is shorter and less cluttered.
extern void swap(int x, int y):
void (* func_ptr)(int x, int y) = f;

funcptr(3, 2);

Just like the two manners of getting a function's address, which way you call a function using a pointer is up to you.

注:
* 1.c的源码:
#include

int main(void)
{
int i = 3;
printf("i resides at %p\n", &i);

return 0;
}

** 2.c的源码:
#include
void func(void);

int main(void)
{
int i = 3;
printf("i resides at %p\n", &i);
printf("func() resides at %p\n", &func);
printf("main() resides at %p\n", &main);

return 0;
}


void func(void)
{
printf("hello world\n");
}

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有