C pointer 3-function pointer

Source: Internet
Author: User

Function pointer

Is a pointer to a function, which can be defined as follows:

INT (* pfunc) (INT, INT );

This statement is interpreted as follows:

First (* pfunc), so pfunc is a pointer;

In another explanation (INT, INT), pfunc points to a function. There are two parameters of this function, both of which are integer;

Finally, return value.

Therefore, if a function int max (int A, int B) is defined, we can use the function pointer as follows:

Pfunc = max; // note the return types of the two, which must be consistent.


Function pointers are different from pointer functions. The latter is a function, and its return value is a pointer, such
Char * index (const char * s, int
C );

INT (* max) (int A, int B); // function pointer

Int * max (int A, int B); // pointer Function

The following code is used for function pointers:

(1)

Int min (int A, int B) {return A> B? B: A;} int max (int A, int B) {return A> B? A: B;} int main () {int A = 12; int B = 34; int (* pfunc) (INT, INT ); // define a function pointer pfunc = min; printf ("min = % d/N", pfunc (A, B); pfunc = max; printf ("max = % d/N", pfunc (A, B); int (* P [2]) (INT, INT); P [0] = min; P [1] = max; printf ("min = % d/N", P [0] (a, B); printf ("max = % d/N ", P [1] (a, B); # define ABC minprintf ("min = % d/N", ABC (a, B )); # define EFG maxprintf ("max = % d/N", EFG (A, B); Return 0 ;}

INT (* P [2]) (INT, INT) is a function pointer array. The function pointer feels similar to # define. See the # define section in the code above.

(2)

Typedef struct point {Double X; Double Y;} Point; int CMP1 (void * a, void * B) {int * c = (int *); int * D = (int *) B; return * C-* D;} int cmp2 (void * a, void * B) {double * c = (double *); double * D = (double *) B; Double E = * C-* D; If (E> 0) return 1; else if (E <0) Return-1; else return 0;} int cmp3 (void * a, void * B) {return strcmp (char *) A, (char *) B );} int cmp4 (void * a, void * B) {point * c = (point *) A; point * D = (Point *) B; Double E = C-> X-D-> X; If (E> 0) return 1; else if (E <0) Return-1; else return 0;} void test (void * a, int N, int size, INT (* p) (void *, void *) {void * P1 =; void * P2 = a + size; // do somethingprintf ("% d/N", P (P1, P2);} int main () {int C [2]; c [0] = 12; C [1] = 34; test (C, 2, sizeof (INT), CMP1); double d [2]; d [0] = 1.2, d [1] = 3.4; test (D, 2, sizeof (double), cmp2 ); char s [2] [16] = {"Xiao", "she "}; Test (S, 2, sizeof (s [0]), cmp3); point PT [2]; Pt [0]. X = 1.8; Pt [0]. y = 1.3; Pt [1]. X = 1.4; Pt [1]. y = 1.5; test (PT, 2, sizeof (point), cmp4); Return 0 ;}in example (2), one of the test functions is a function pointer, call different comparison functions to process different types of data.

Is it similar to qsort?

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.