C / C ++ function pointers and function pointers

1, the function pointer and

A. First, a brief look and function pointers

Briefly, the function is to complete a single function block, which is a type of return value of the function name + + (parameter) as well as the composition of the specific code (believe we know everything!); And a pointer (pointer variable) is a storage addresses (number of memory cells) of the variable, and therefore the content by the respective pointer may address.

2, the function pointer

Pointer function refers to the return value type is a pointer type function, in essence, is ultimately the function will return an address or address expression to the calling function. Its essence is still a function!

Which is a function prototype: Type * function name (formal parameters)

Such as selecting the maximum value of the two values and returns its address
Here Insert Picture Description
output is:

000000000065FE48

000000000065FE48

Why these two locations are the same? The reader to think about.

Talk about which two things:

1. The pointer type must return type of the function return value and the like, as in the above type of function return value is max int type, it must be followed by return int type

2. Return pointer address must be valid i.e., the pointer function can not return a pointer to local variables in the function, such as

int*f(){ int i= 7;return &i;}

It is not legitimate, because i is a local variable, its life cycle will end with the end of the f function and, when complete execution function f, i occupied memory will be freed, that it has no address a. (On the contents of memory, the reader is referred to the four memory area of ​​a number of public this text) but can return to legitimate addresses, such as global variables, such as the address of a static variable.

Action pointer to the function:

The code can be more concise and save memory to some extent; such as when you need to return an array of elements, you simply return the address of the first element to the calling function, call the function to operate the array (do I return multiple values).

Or dynamically allocated memory malloc function, which returns the memory address to another function, another function to do the operation of the memory. Of course, there are other effects, the reader own experience in practice.

3, function pointer

First, to help you sort out several concepts

1. function address

Functions, applications, and so is the object of compiler processing functions are compiled after the section of the code, the code for the system immediately allocate some storage space, storage space and the first address of this entry is the address of the function or what we say function pointer. (Function is stored in the code area) is thus a function pointer to the corresponding function.

A function has been defined, it is his function name or address entry function pointer. So the concept with function pointers, in addition to our call to the function is called after a name, is not there another way?
Here Insert Picture Description
The result:

just test! just test! just test!

4014F0 4014F0 4014F0

Why can & f function name and other operations are the same address and the final output?

This is because when the function names appear in an expression, the compiler will automatically be converted to a pointer, the pointer to the function. (Vulgar or function name that is a pointer pointing to itself) Thus f, & f * & f and corresponds to the entry address of the function.

When a function is finalized, it will settle down memory space, which means that the corresponding address is determined that the function name is a constant address, then you can assign it to the pointer to a function.

2. The function pointer

Function name represents a function code directly address in memory, it is possible to obtain the address of the pointer variable used in the function, the variable pointer to a function referred to as function pointers.

Before representation describes the function pointer, the first to understand what type of function.

Function type is a type and parameters of the return type of the function, which is a function of the interface.

The int test1 (int *, double), and int test2 (int *, double) is the same type of function, float test3 (int, int) with the test1 and test2 is not the same type of function. test1 and test2 interface type int (int *, double)

We can use the keyword typedef to abstract one type of function, representation:

typedef return type function type name (parameter type form)

Type the name of the function is a custom identifier that is defined by the user.

The typedef int f (int *, double) is int (int *, double) the function of this type of abstraction.

I.e., f represents the int (int *, double) this type of function.

It can be used f test1, test2; do test1 and test2 the prototype declaration.

Next we introduce representation of function pointers:

Return Value (* pointer variable name) (formal parameter types)

Because the function pointer variable is a pointer to a certain class of functions, it can be so expressed:

Type the name of the function pointer type *

So more for the same function pointer type, so what we succinct way means no?

We still have to define a pointer type with typedef:

typedef int (*P)(int,int)

Well, look at the code below
Here Insert Picture Description
outputs are all 2 00000000004015C5

note

Function pointer int (* p) (int, int) in the (* p) parenthesis can not be removed, or else it becomes a

Int * p (int, int) function pointer.

4, the application function pointers

We say that a long list, the function pointer in the end what use is it? Generally speaking there are two

1. Function call function pointer

2. The general function pointer as the pointer function of the same parameters as in the form of

An example of it is not as good as a thousand words.

Example: two numbers and arithmetic symbols input from the keyboard, the result of these two output operands
Here Insert Picture Description
output:

please select your operation(input+or-)+

please input the two operand

3

4

the operation is :3.000000+4.000000=7.000000

Of course, more advanced function pointer action, as in a hierarchical system abstraction and design effects.

Published 261 original articles · won praise 4 · Views 4258

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105206448