C function pointer simple use case, function pointer use case

Source: Internet
Author: User

C function pointer simple use case, function pointer use case

(1) function pointer:
Pointer to a function of the fixed parameter type and Return Value Type
Statement:
Int fun (int, int)
|
\/
Int (* pfun) (int, int)

Pfun is the function pointer (variable)

Call:

D = pfun (a, B );
E = (* pfun) (a, B );

##### Pointer Functions
Int * fun (int, int );
Int * (fun (int, int ));
#####

 

/* Ex01.c */
# Include <stdio. h>

Int max (int a, int B ){
Return (a> B? A: B );
}
Int min (int a, int B ){
Return (a <B? A: B );
}

Int main (int argc, char * argv []) {
Int (* pfun) (int, int );
Pfun = max;
Int a = 0, B = 0, c = 0, d = 0, e = 0;
Fscanf (stdin, "% d", & a, & B );

C = max (a, B );
D = pfun (a, B );
E = (* pfun) (a, B );
Fprintf (stdout, "% d \ n", c );
Fprintf (stdout, "% d \ n", d );
Fprintf (stdout, "% d \ n", e );
Return 0;
}



(2) define the type of the function pointer and declare the function pointer variable.
Typedef int (* Pfun) (int, int );

Pfun is the pointer type of a user-defined function.

Pfun pfun = min;

Create a Pfun variable and assign the min function to it.

 

/* Ex02.c */
# Include <stdio. h>

Typedef int (* Pfun) (int, int );

Int max (int a, int B ){
Return (a> B? A: B );
}
Int min (int a, int B ){
Return (a <B? A: B );
}

Int main (int argc, char * argv []) {
Pfun pfun = min;
Int a = 0, B = 0, c = 0, d = 0, e = 0;
Fscanf (stdin, "% d", & a, & B );

C = min (a, B );
D = pfun (a, B );
E = (* pfun) (a, B );
Fprintf (stdout, "% d \ n", c );
Fprintf (stdout, "% d \ n", d );
Fprintf (stdout, "% d \ n", e );
Return 0;
Return 0;
}


(3) You can pass a function as a parameter to another function.
# Include <stdio. h>

Typedef int (* Pfun) (int, int );

Int max (int a, int B ){
Return (a> B? A: B );
}
Int min (int a, int B ){
Return (a <B? A: B );
}

Int dofun (Pfun pfun, int a, int B) {// use the Pfun type function pointer variable pfun to receive incoming Functions
Return pfun (a, B );
}

Int main (int argc, char * argv []) {
Pfun pfun = min;
Int a = 0, B = 0, c = 0, d = 0, e = 0, f = 0;
Fscanf (stdin, "% d", & a, & B );

C = min (a, B );
D = pfun (a, B );
E = (* pfun) (a, B );
F = dofun (min, a, B); // input the min function as a parameter to dofun. dofun receives the function pointer of the corresponding type.
Fprintf (stdout, "% d \ n", c );
Fprintf (stdout, "% d \ n", d );
Fprintf (stdout, "% d \ n", e );
Fprintf (stdout, "% d \ n", e );
Return 0;
Return 0;
}


C function pointer example

Mxl033 is correct.
Void * func (void) is not a function pointer, but a function that returns a pointer. Is a function, not a pointer.

Typedef void (* pFunc) (); // function pointer type declaration, pointing to a function without returning any parameters.
Typedef int (* pFunc1) (int, int); // function pointer type declaration, pointing to return int type. The parameter has two int type functions.

Void func1 ()
{
Printf ("This is func1 \ n ");
}

Int add (int a, int B)
{
Return a + B;

}
Int main (void)
{
PFunc p = func1; // function pointer copy
P ();

// P = add; error, return type, and parameter do not match

PFunc1 p1 = add;

P1 (1, 2) // returns 3

Return 0;
}

The landlord can learn more slowly.

How to call C function pointers? How to use function pointers? An example is recommended.

Function: int fun (int a, int B );
To define a pointer to this function
Compare the pointer pointing to int;
Int * p; p = &;
How is p defined?
Make sure that p is a pointer type.
Write (* p ),
Then, consider the base type of p,
The base type of p is the int type of variable.
Put int in front of (* p ).
Int (* p );
Parentheses can be omitted, which is int * p;

Likewise
To implement pf = & fun;
(* Pf) define pf as a pointer,
Use the fun type as the base type of pf
Fun is equivalent to an int (int a, int B) type.
Int (int a, int B) (* pf );
The base type contains parentheses and brackets to be removed.
Int (* pf) (int a, int B); // brackets cannot be omitted
Pf = & fun;
Call time
(* Pf) (); pf () can both

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.