Functions and pointers, function pointers
# Include <stdio. h>/* function and pointer * // function declaration char * getString (); int sum (int, int); int main () {// 1. pointer function char * string = getString (); printf ("% s \ n", string); // 2. function pointer printf ("% p \ n", sum); // indicates that the function name is the address of the function // int a; // int * p = &; // define the pointer p, type int (*) (int a, int B) int (* p) (int a, int B) = sum; // call the function using the function pointer // int result = (* p) (2, 3); int result = p (2, 3); printf ("result: % d \ n ", result); return 0;} // 1. the pointer function // returns the string char * getString () {char * s = "Hello, world"; // return address return s;} int sum (int a, int B) {return a + B ;}
What is the difference between a function pointer and a pointer function?
The pointer of a function, for example, int (* p) (); that is, the pointer to the function. The p function returns an integer value. The pointer function is in the form of int * p (); that is, the "function" of the pointer value is returned, which points to the integer data. If you do not understand these two sentences, you will appreciate them.
Where is the difference between a function pointer and a pointer?
Pointer is a type of data in C language;
A function pointer is a type of C pointer.
Q: "What is the difference between a function pointer and a pointer ?" It indicates that LZ is not familiar with pointers. Modify your question:
"What is the difference between a function pointer and an integer pointer ?"
In this case, I will tell you:
1. Both are pointers
2. The function pointer stores the first address of a function, while the integer pointer stores the address of an integer variable;
3. the pointer itself is a reference type. Therefore, you must release the reference when using it. Function pointers are different from integer pointers for removing references. There are two ways to unreference an integer pointer:
For example, for integer pointer pi:
Int I = 0;
Int * pi = & I;
Method 1: * pi
Method 2: pi [0]
Function pointer pf:
Int f (int );
Int (* pf) (int) = f;
Release reference method: pf (8 );
Method 2 for unreferencing integer pointers: pi [0] is more like an array. It is also more like the method for removing the reference from the function pointer because:
The array name and function name are essentially addresses. Pointers are essentially addresses.
4. In the preceding example, the assignment format is different.
Function pointer pf = f; (no &. In fact, the same is true)
Integer pointer pi = & I;
The reason is that the array name and function name are essentially addresses, while the essence of an integer variable (when used as the left value [if allowed] or the right value) is the value in the address.
5. For function pointers, incremental operations such as pf ++ 99.9999% are generally not allowed.
To access the Function Array, use the function pointer array.