In C: Pointers are features of the C language, with various pointers, common variable pointers, constant pointers, array pointers, pointer arrays, function pointers, pointer functions. Let's just talk about function pointers and callback functions.
The first thing about function pointers is actually very simple.
For a function pointer, as the name implies, is a pointer to a function, need to know that, for the pointer, he always stores a piece of address, the address has a, a group, or a piece of data, in the function, the function of the storage is placed in the code snippet, each function has a functional first address, Call this function equivalent to the call of this address.
Specifically, you can watch my blog, where a restart function is successfully invoked by changing the return value of the stack frame in the memory phase. Light 谈栈帧 (I.)
In fact, the virtual function table in C + + features is used.
So, the function pointer is a pointer to the first address of the functions, OK, first understand this definition, and then we go to the next stage.
For pointers, he doesn't just point to a specific amount, he also needs other identifiers to describe the specific properties of the volume.
For example: char* str,void *func (int,int);
Around the pointer symbol *, is the specific information describing the pointer.
So what is this for a function pointer?
void (*f) ();Although()priority is higher than*, but due to the existence of parentheses, the first thing to do is to dereference, soFis a pointer; next executes( ), indicatingFpoint to a function that does not return any values. Now come to the conclusion that:Fis a pointer to a function that does not accept a parameter and returns no value, for short, a function pointer(pointer to function).
<1>. Initialize
note pointers to functions ( function pointer ) Refers to a function rather than a normal variable, and it points to a function that has a specific type, and the type of the function is determined by its return value type and formal parameter list, regardless of the function name. You can use function names or function pointers of the same type function when initializing a function pointer ( and, of course, 0 pointer constants ). If there is a function void Test (),int wrong_match (int) and function pointer void (*PTF) ().
The following initialization is wrong because the type of the function pointer does not match the type of the function:
f = wrong_match;
f = & Wrong_match;
PTF = Wrong_match;
PTF = & Wrong_match;
The following initialization and assignment are legal:
f = test;
f = &test;
PTF = test;
PTF = &test;
f = PF;
to make an explanation,Testand the&testcan be used to initialize a function pointer. The C language specifies that the function name will be converted to a pointer to the function, unless the function name is used as the operand of the & operator or the sizeof operator ( Note: The operand of the function name for sizeof is illegal .) Which meansf = test;inTestis automatically converted to&test, whilef= &test;has been shown to use the&test, soTestthere will be no more conversions. Therefore, a direct reference to the function name is equivalent to applying the function name&operator, both methods will get a pointer to the function.
int (*function (int))) (Double*,char); to understand the meaning of this declaration, first look at function (int), the function is declared as one, it has a formal parameter of type int, the return value of this function is a pointer, It is the function pointer Int (*) (double*, char) that we would have spoken at the beginning; this pointer points to a function that returns an int with two parameters, respectively, double* and char types. If you use typedef , you can simplify this declaration:
typedef int (*PTF) (double*, char);
PTF function (int);
Summarize:
In fact, for a function pointer. We just have to remember. The function pointer is (*) from the beginning of the middle bracket to the outside step.
The pointer function is (* ()) and knows how to partition, knowing what the function pointer is. Figure out his name and figure out what he's describing.
callback function:
It has been made clear that for a function pointer, he has his own name, has a relative description of the information, the information is not the same as the assignment can not be paired, and then since there is a function pointer this thing? So what does he do for a show?
In fact, for the function pointer, nothing is to let us in the face of different situations to use him. The function pointer points to a different case, to do different things, to provide a different function pointer in the topic function processing excuse, to handle the different situations.
in theComputer programmingin whichcallback function, or ShortCallback(Callback, which is called and back by the main function call), refers to thefunction parametersa piece of code passed to anotherExecutable codeof theReference. This design allows theUnderlyingcode calls are defined in a high-levelSub-Program.
Consider a simple example:
#include <stdio.h>int int_cmp (const void * p1, const void * &NBSP;P2) { if (* ( int *) p1 > * (int *) p2) { return 1; } else if (* ( int *) p1 == * (int *) p2) { return 0; } else { return -1; }}void _swap (void *p1, void * p2, int size ) { int i = 0; for (i = 0; i < size; i++) { char tmp = * ((char *) P1&NBSp;+ i); * (( char *) p1 + i) = * (( char *) p2 + i); * ( char *) p2 + i) = tmp; }}void bubble (void *base, int count , int size, int (*cmp ) (void *, void *)) { int i = 0; int j = 0; for (i = 0; i< count - 1; i++) { for (j = 0; j<count - i - 1; j++) { if (cmp (char *) base + j*size , (char *) base + (j + 1) *size) > 0) { _swap ( char *) base + j*size, (char *) base + (j + 1) *size, size); } } }}int Main () { int arr[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 0 }; //char *arr[] = {"AAAA", "dddd", " CCCC "," bbbb "}; int i = 0; bubble (arr, sizeof (arr) / sizeof (arr[0]), sizeof (int), int_cmp); for (i = 0; i< sizeof (arr) / sizeof (arr[0]); i++) { printf ( "%d ", arr[i]); } printf (" \ n "); return 0;}
This article is from the "egg-left" blog, please be sure to keep this source http://memory73.blog.51cto.com/10530560/1771227
"C language" function pointers and callback functions