function pointers in the C language

Source: Internet
Author: User

1. Definition of function pointers

As the name implies, a function pointer is a pointer to a function. It is a pointer to a function.


2. Use of function pointers

We define a function pointer, but how do we use it? Let's look at the following example:

#include <stdio.h>

#include <string.h>

char * Fun (char * p1, char * p2)

{

int i = 0;

i = strcmp (P1, p2);

if (0 = = i)

{

return p1;

}

Else

{

return p2;

}

}

int main ()

{

char * (*PF) (char * p1, char * p2);

PF = &fun;

(*PF) ("AA", "BB");

return 0;

}


3. Array of function pointers

Now we know that the expression "char * (*PF) (char * p)" Defines a function-pointer pf. Since PF

is a pointer, it can be stored in a number of groups. To modify the above style:

char * (*pf[3]) (char * p);

This is the definition of a function pointer array. It is an array with an array named PF, and the array is stored in 3

That points to the function

Pointer. These pointers point to a pointer with a return value type pointing to a character, a parameter of billions of points to a character

The function of the pointer

Number. It seems a bit awkward to read. But it doesn't matter, the point is you know this is an array of pointers,

is an array.

How does an array of function pointers work? Also gives a very simple example, as long as the real master of the

Use method,

Complex problems can be addressed. As follows:

#include <stdio.h>

#include <string.h>

char * FUN1 (char * p)

{

printf ("%s\n", p);

return p;

}

char * FUN2 (char * p)

{

printf ("%s\n", p);

return p;

}

char * FUN3 (char * p)

{

printf ("%s\n", p);

return p;

}

int main ()

{

char * (*pf[3]) (char * p);

Pf[0] = fun1; Function names can be assigned directly

PF[1] = &fun2; You can add address characters to the function name

PF[2] = &fun3;

Pf[0] ("fun1");

Pf[0] ("fun2");

Pf[0] ("fun3");

return 0;

}

4. Application of the array of function pointers (implementation of the simple Calculator):

#include <stdio.h>

int add (int a, int b)

{

return a + B;

}

int sub (int a, int b)

{

return a-B;

}

int mul (int a, int b)

{

return a*b;

}

int div (int a, int b)

{

return a/b;

}

int main ()

{

int x, y;

int input = 1;

int ret = 0;

while (input)

{

printf ("*************************\n");

printf ("1:add 2:sub \ n");

printf ("3:mul 4:div \ n");

printf ("*************************\n");

printf ("Please select:");

scanf ("%d", &input);

switch (input)

{

Case 1:

printf ("Input operand:");

scanf ("%d%d", &x, &y);

RET = Add (x, y);

Break

Case 2:

printf ("Input operand:");

scanf ("%d%d", &x, &y);

RET = sub (x, y);

Break

Case 3:

printf ("Input operand:");

scanf ("%d%d", &x, &y);

RET = Mul (x, y);

Break

Case 4:

printf ("Input operand:");

scanf ("%d%d", &x, &y);

ret = div (x, y);

Break

Default

printf ("Select Error \ n");

Break

}

printf ("ret =%d\n", ret);

}

return 0;

}

Use the implementation of a function pointer array:

#include <stdio.h>

int add (int a, int b)

{

return a + B;

}

int sub (int a, int b)

{

return a-B;

}

int mul (int a, int b)

{

return a*b;

}

int div (int a, int b)

{

return a/b;

}

int main ()

{

int x, y;

int input = 1;

int ret = 0;

Int (*p[5]) (int x, int y) = {0, add, Sub, mul, Div}; Transfer table

while (input)

{

printf ("*************************\n");

printf ("1:add 2:sub \ n");

printf ("3:mul 4:div \ n");

printf ("*************************\n");

printf ("Please select:");

scanf ("%d", &input);

if ((Input < 4 && input > 1))

{

printf ("Output operand:");

scanf ("%d%d", &x, &y);

ret = (*p[input]) (x, y);

}

Else

printf ("Incorrect input \ n");

printf ("ret =%d\n", ret);

}

return 0;

}


This article is from the "Pawnsir It Road" blog, so be sure to keep this source http://10743407.blog.51cto.com/10733407/1749358

function pointers in the C language

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.