转一篇关于函数指针的文章

发表于:2007-05-25来源:作者:点击数: 标签:不怕一篇指针文章关于
不怕您见笑,以前学c语言,一直都没搞清楚过函数指针的问题,直到我发现了这篇文章:) 不怕您见笑,以前学c语言,一直都没搞清楚过函数指针的问题,直到我发现了这篇文章:) 作者:Peter 出处:http://www.dirac.org/linux/programming/tutorials/function
不怕您见笑,以前学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");
}

原文转自:http://www.ltesting.net

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)