What are the differences between pointer functions and function pointers?

Source: Internet
Author: User

What is the difference between pointer functions and function pointers (reprinted)
Published by: 14:23:02 Source: yourblog.org)
What are the differences between pointer functions and function pointers?

1. These two concepts are short for short. A pointer function refers to a function with a pointer, that is, a function in essence. We know that all functions return a type (if no value is returned, it is null), but the pointer function returns a pointer of a type. The definition format is as follows:

Return type identifier * return name (form parameter table)
{Function body}

The return type can be any basic type or composite type. The function that returns pointers is widely used. In fact, every function, even if it does not return a pointer of a certain type, has an entry address, which is equivalent to a pointer. For example, if a function returns an integer value, it actually returns the value of a pointer variable. However, the variable is the function itself, and the whole function is equivalent to a "variable ". For example, the following example returns a pointer function:

# Include

Float * Find ();
Main ()
{
Static float score [] [4] = {60, 70,}, {56, 89 }};
Float * P;
Int I, m;
Printf ("Enter the number to be found :");
Scanf ("% d", & M );
Printf ("the score of No. % d are:/N", M );
P = find (score, M );
For (I = 0; I <4; I ++)
Printf ("% 5.2f/T", * (p + I ));
}

Float * Find (float (* PIonter) [4], int N)/* define pointer function */
{
Float * PT;
PT = * (PIonter + n );
Return (PT );
}

The student ID starts from 0. The find () function is defined as a pointer function. The start parameter pointer is a pointer variable pointing to a one-dimensional array containing four elements. Pointer + 1 points to the first line of score. * (Pointer + 1) points to the first row's 0th elements. PT is a pointer variable that points to a floating point variable. Call the find () function in the main () function and pass the first address of the score array to pointer.

2. "function pointer" is a pointer variable pointing to a function. Therefore, "function pointer" should be a pointer variable first, except that the pointer variable points to a function. This is just like using pointer variables to point to integer variables, struct variables, and arrays. Here we point to functions. As mentioned above, during C compilation, each function has an entry address, which is the address pointed to by the function pointer. With the pointer variable pointing to the function, you can use the pointer variable to call the function, just like using the pointer variable to reference other types of variables. Function pointers have two purposes: Calling functions and making function parameters. Function pointers are described as follows:
The data type identifier (* pointer variable name) (parameter). Note: parameters in function brackets are optional, depending on the situation.
The following program illustrates how a function pointer calls a function:

# Include

Int max (int x, int y) {return (x> Y? X: Y );}

Void main ()
{
INT (* PTR )();
Int A, B, C;
PTR = max;
Scanf ("% d, % d", & A, & B );
C = (* PTR) (A, B );
Printf ("A = % d, B = % d, max = % d", A, B, C );
}

PTR is a pointer variable pointing to a function. Therefore, you can assign the max () function to PTR as the PTR value, that is, assign the entry address of max () to PTR, later, we can use PTR to call this function. In fact, both PTR and max point to the same entry address. The difference is that PTR is a pointer variable, which is not as dead as the function name, it can point to any function, just like how you do it. In a program, the address of a function is assigned to it, and it points to the function. Then call it with the pointer variable, so you can point to different functions successively. However, note that the pointer variable pointing to the function does not have ++ and -- operations, so be careful when using them.

Junglesong reprinted www.chinagpa.com

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.