Next: Assigning Function Pointers, Up: Function Pointers [Contents][Index]
The declaration of a function pointer variable (or structure field)
looks almost like a function declaration, except it has an additional
‘*’ just before the variable name. Proper nesting requires a
pair of parentheses around the two of them. For instance, int
(*a) ();
says, “Declare a
as a pointer such that *a
is
an int
-returning function.”
Contrast these three declarations:
/* Declare a function returningchar *
. */ char *a (char *); /* Declare a pointer to a function returningchar
. */ char (*a) (char *); /* Declare a pointer to a function returningchar *
. */ char *(*a) (char *);
The possible argument types of the function pointed to are the same as in a function declaration. You can write a prototype that specifies all the argument types:
rettype (*function) (arguments…);
or one that specifies some and leaves the rest unspecified:
rettype (*function) (arguments…, ...);
or one that says there are no arguments:
rettype (*function) (void);
You can also write a non-prototype declaration that says nothing about the argument types:
rettype (*function) ();
For example, here’s a declaration for a variable that should
point to some arithmetic function that operates on two double
s:
double (*binary_op) (double, double);
Structure fields, union alternatives, and array elements can be function pointers; so can parameter variables. The function pointer declaration construct can also be combined with other operators allowed in declarations. For instance,
int **(*foo)();
declares foo
as a pointer to a function that returns
type int **
, and
int **(*foo[30])();
declares foo
as an array of 30 pointers to functions that
return type int **
.
int **(**foo)();
declares foo
as a pointer to a pointer to a function that
returns type int **
.
Next: Assigning Function Pointers, Up: Function Pointers [Contents][Index]