Function pointer questions, answers, function pointer questions,
In the codeStatements 1, 2, and 3And use the function pointer to call the FunctionStatement a and BWhich of the following statements is correct ???
# Include <stdio. h> int add (int a, int B) {return a + B;} int main () {int (* p) (int, int); int sum; p = add; // Statement 1 // p = & add; // Statement 2 // p = * add; // statement 3sum = p (1, 2 ); // Statement a // sum = (* p) (a, B); // statement bprintf ("% d \ n", sum); return 0 ;}
C language pointer
First, define the function pointer
A and D are defined as follows: A defines the pointer to the function pf. The parameter to which the function is directed is null and no return value.
D defines the pointer to the function pf. The function parameter is int or char (the title is int or char *) and no return value is returned.
B. D is defined as: Define the function pf. The function parameter is null, And the return type is void * (void pointer)
The definition of a single-slave function pointer can exclude BCD.
Second, pointer assignment
The function name and array name are similar in nature: the address of the first data in the array, that is, a [10], then a = & a [0]
The name of the function is the memory address of the first instruction in the function body ~~ So we do not need to use the address operator during replication!
Hope to help you.
Function pointer: A simple C program does not know why an error is reported during compilation.
Hello!
Because the returned values of filefunc and editfunc are of the void type.
It does not match the return value type int of the function pointer you defined, so you can change int to void.
# Include <stdio. h>
# Include <stdlib. h>
Void filefunc ()
{
Printf ("njksjk \ n ");
}
Void editfunc ()
{
Printf ("nsjknjk \ n ");
}
Int main ()
{
Typedef void (* funcp) (); // change void to int
Funcp p = filefunc;
P ();
P = editfunc;
P ();
System ("pause ");
Return 0;
}
Hope to help you