I personally understand pointer and function pointers in C language.
1. function pointer
As the name suggests, it refers to the pointer to the function. The function is the same as other pointers. the pointer variable stores the address of the function to which it points.
For example, the void type function pointer can be defined by void (* f) (parameter list), or typedef void (* F) (parameter list ),
F f. Note that the type of the function pointer must be the same as that of the function.
The following is a simple example.
1 # include <stdio. h> 2 # include <stdlib. h> 3 typedef void (* F) (); 4 5 void print_hello () // simply define a print function with no parameters 6 {7 printf ("hello ~ \ N "); 8} 9 10 void main () 11 {12 F f1; // This is equivalent to void (* f1) (); 13 void (* f2 )(); 14 15 f1 = print_hello; 16 f2 = print_hello; 17 18 f1 (); 19 f2 (); 20 21 system ("pause"); 22}
View Code
2. pointer Functions
A pointer function is a function that returns a pointer. For example, int * a () and char * B ()... indicate functions with integer pointers and struct pointers respectively.
The following is a simple example.
# Include <stdio. h> # include <stdlib. h> char * str () // define a function that returns a character pointer {char * a = "hello world ~ "; Return a;} void main () {char * c = str (); // you can assign a value to char * B at the same time when defining it; // You can also define it first, then assign B = str (); printf ("% s \ n", c, B); system ("pause ");}
View Code