Function pointers and pointer Functions

Source: Internet
Author: User

Function pointers and pointer Functions
1. function pointer

Pointer to the function. Essentially a pointer.

A pointer variable can point to the address, array, string, and dynamic allocation address of a variable. It can also point to a function. during compilation, the system assigns an entry address to the function,Function name indicates the entry addressThe pointer variable pointing to the function is called the function pointer variable.

Introduction: int sumup (int a, float B); int (* p3) (int a, float B); // Add p3 = & sumup; then p3 points to sumup as a pointer. This is the p3 pointer pointing to the function [Review: both the returned values and the form parameters are int (int a, float B);] int * (* p4) (int a, float B ); in terms of type, int * (int a, float B) is the pointer of the above function. Therefore, p4 = & p3;

Int (* p) (int x); // declare a function pointer

// You can assign the first address of the func function to the pointer p in the following two ways:

P = func;

P = & func;

It is not necessary to use the address operator, because a Function Identifier represents its address. If it is a function call, it must also contain a parameter table enclosed in parentheses. You can use the following two methods to call a function through a pointer:

X = (* p )();

X = p (); // although it seems similar to a normal call, some programmers tend to use the first format because it explicitly points out that the function is called through pointers rather than function names.

For example, the example in the reference is called as follows: sumup (100, 1.21); (* p3) (100, 1.21); // equivalent to * (p4) (100, 1.21); // equivalent to the above

Example program:

Void (* funcp) (); void FileFunc (), EditFunc (); main () {funcp = FileFunc; (* funcp) (); funcp = EditFunc; (* funcp) ();} void FileFunc () {printf (FileFunc \ n);} void EditFunc () {printf (EditFunc \ n);} the program output is: FileFunc EditFunc

  

2. pointer functions refer to functions with pointers, That is, the essence is a function.. Function returnType YesA pointer of a certain type, that is, an address is returned to the calling function to be used in the expression that requires a pointer or address. . Type identifier * function name (parameter table)For example: Declaration: int * f (x); int * p; call: p = f ();
Int * GetDate (int wk, int dy) // return the address of an element {static int calendar [5] [7] = {1, 2, 3, 4, 5, 6, 7 }, {8, 9, 10, 11, 12, 13, 14}, {15, 16, 17,18, 19,20, 21}, {22, 23, 24, 25, 26, 27, 28}, {29, 30, 31,-1 }}; return & calendar [wk-1] [DY-1];} main () {int wk, dy; do {printf (Enter week (1-5) day (1-7) \ n); scanf (% d, & wk, & dy );} while (wk <1 | wk> 5 | dy <1 | dy> 7); printf (% d \ n, * GetDate (wk, dy ));}

  

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.