[C Language] function pointer

Source: Internet
Author: User

[C Language] function pointer
A pointer can point to a variable, an array, or a function. A function pointer is a pointer to a function. The function name is actually the starting address of the program in the memory. The pointer to the function can pass the address to the function or return the pointer to the function from the function. For example, a function is used to calculate the sum of two numbers and call the function through the function pointer. # Include <stdio. h> int sum (int a, int B); // Declaration of the summation function void main () {int a, B; int (* fun) (int, int ); // declare a function pointer printf ("enter two integers:"); scanf ("% d, % d", & a, & B ); printf ("the first method to call a function: the function name calls the sum function: \ n"); printf ("% d + % d = % d \ n", a, B, sum (a, B); // call fun = sum through the function name; // The function pointer points to the summation function printf ("the second method to call a function: function pointer calls the summation function: \ n "); printf (" % d + % d = % d \ n ", a, B, (* fun) (, b); // call the function through the function pointer} int sum (int m, int n) // the result of the {return m + n;} program running is as follows: the statement int (* fun) (in T, int); declares a pointer variable pointing to the function and returns an integer value. There are two integer parameters. Statement fun = sum indicates that the function pointer fun points to the function sum. both fun and sum point to the start address of the function sum. during compilation, the program is translated into a line of commands and loaded into the memory area. The statement (* fun) (a, B) in the main function is used to call the summation function. You can also write it as fun (a, B) because the function itself is an address.

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.