2008年12月17日 星期三

Pointers to Functions

§Pointer to function
Contains address of function
Similar to how array name is address of first element
Function name is starting address of code that defines function

§Function pointers can be
Passed to functions
Stored in arrays
Assigned to other function pointers

§Example: bubblesort
Function bubble takes a function pointer
-bubble calls this helper function
-this determines ascending or descending sorting
The argument in bubblesort for the function pointer:
int ( *compare )( int a, int b ) tells bubblesort to expect a pointer to a function that takes
two ints and returns an int
If the parentheses were left out: int *compare( int a, int b )
-Defines a function that receives two integers and returns a pointer to a int
Function Pointers:


Pointers to Functions:


沒有留言: