Basic knowledge of C + +: Fundamental concepts of function pointers and pointer functions

Source: Internet
Author: User
Tags define function

"Function pointer"

In the program run, the function code is the program's algorithm instruction part, they and the array also occupies the storage space, has the corresponding address. You can use pointer variables to point to the first address of an array, or you can use a pointer variable to point to the first address of a function code, which is called a function pointer to the first address of the function code.

1. function pointer definition

function type (* pointer variable name) (parameter list);

The function type describes the return type of the function, because the precedence of "()" is higher than "*", so the parentheses outside the pointer variable name are necessary, and the subsequent "parameter list" represents the parameter list of the function that the pointer variable points to.

For example:

Int (*f) (int x);

Double (*ptr) (double x);

When defining a function pointer, note that:

The number and type of parameters of the function pointer and the function it points to should be-to;

The type of the function pointer and the return value type of the function must also be consistent.

2. Assigning values to function pointers

The function name and array name represent the first address of the code, so the function pointer is directly directed to the function name when the value is assigned.

For example

int func (int x); /* Declare a function */

Int (*f) (int x); /* Declare a function pointer */

F=func; /* Assigns the first address of the Func function to the pointer f */

The function func is assigned with no parentheses and no arguments, since Func represents the first address of the function, so after the assignment, the pointer F points to the first address of the code of the function func (x).

3. Calling a function from a function pointer

function pointers are called by function names and related parameters.

Similar to other pointer variables, if the pointer variable pi is a pointer to an integer variable i, *p is equal to the variable i it refers to, and if PF is a pointer to a floating-point variable F, then *PF is equivalent to the variable F it refers to. Similarly, *f is a pointer to the function func (x), then *f represents the function func it points to. So F=func is executed, and then (*f) and func represent the same function.

Because the function pointer points to a function in the store, you can call the corresponding function through a function pointer. Now let's talk about how to call a function with a function pointer, which should perform the following three steps:

First, you describe the function pointer variable.

For example: Int (*f) (int x);

Second, assign a value to the function pointer variable.

For example: F=func; (func (x) must be defined first)

Finally, use (* pointer variable) (parameter table); Call function.

Example: (*f) (x);(x must be assigned first)


"Example" arbitrarily enter n number, find the maximum number, and output the maximum value.

Main ()

{

int f ();

int i,a,b;

Int (*p) (); /* Define function pointer */

scanf ("%d", &a);

P=f; /* Assign a value to the function pointer p, so that it points to the function f */

for (i=1;i<9;i++)

{
scanf ("%d", &b);

A= (*p) (A, b); /* Call function f through pointer p */

}

printf ("The Max number is:%d", a)

}


f (int x,int y)

{

int z;

z= (x>y) x:y;

return (z);

}

The result of the operation is:

343-45 4389 4235 1-534 988 555 789

The Max number is:4389


"Pointer function"

A function can not only bring back the value of an integer data, a character type value and a value of a real type, but also bring back the data of the pointer type to point to an address cell.

A function that returns a pointer, in the general definition format:

Type identifier * Function name (parameter table)

int *f (x, y);

where x, y is the formal parameter, F is the function name, and the call returns an address pointer to the integer data. f (x, y) is a function whose value is a pointer.

For example: Char *ch (), a function that returns a character pointer, see the following example:

"Example" copies the string 1 (str1) to String 2 (STR2) and outputs the string 2.

#include "stdio.h"

Main ()

{

Char *ch (char *,char *);

Char str1[]= "I am glad to meet you!";

Char str2[]= "welcom to study c!";

printf ("%s", CH (str1,str2));

}

Char *ch (char *str1,char *str2)

{

int i;

Char *p;

P=str2

if (*str2==null) exit (-1);

Do

{

*STR2=*STR1;

str1++;

str2++;

}while (*str1!=null);

return (p);

}


Through analysis can be obtained

A function pointer is a pointer to a function, and the pointer function simply means that he is a function that returns a pointer.

A function pointer can be used to point to a function.

Reference: http://www.kuqin.com/language/20080614/9502.html

Basic knowledge of C + +: Fundamental concepts of function pointers and pointer functions

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.