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

 

Function Pointers

Here's the code used for this tutorial. I won't insult your intelligence by telling you that you should compile and play with these programs as you follow the text.

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.

#include <stdio.h>

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

   return 0;
}
              


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.

#include <stdio.h>
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");
}
              



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.

The problem is that parenthesis in function calls have higher precedence than the dereference operator.

(incorrect)Pointer to objectPointer to function
declaration:
definition:
int i;
int *int_ptr = &i;
int f(int arg);
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 objectPointer to function
declaration:
definition:
int i;
int *int_ptr = &i;
int f(int arg);
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.

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

 extern long double myfunc1(int arg);
 long double (*func_ptr)(void) = myfunc1;


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

 extern double myfunc2(char *str);
 char (*func_ptr)(char *str) = myfunc2;


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

 extern char *myfunc3(int *arg);
 char *(*func_ptr)(int *arg) = myfunc3;


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;

	(*funcptr)(3, 2);


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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值