Differences between pointer functions and function pointers

Source: Internet
Author: User

Differences between pointer functions and function pointers

I,

When I learned about arm, I found that the "pointer function" and "function pointer" are easy to make mistakes. So today, I want to figure it out and find some information, first, the definitions between them:

1. A pointer function is a function with a pointer. A function returns a pointer of a certain type.

Type identifier * function name (parameter table)

Int * f (x, y );

 

First, it is a function, but the return value of this function is an address value. Function return values must be accepted by pointer variables of the same type. That is to say, pointer functions must have function return values. In addition, in the main function, function return values must be assigned to pointer variables of the same type.

Indicates:

Float * fun ();

Float * p;

P = fun ();

Note that pointer functions and function pointers indicate different methods. Do not confuse them. The simplest way to identify a function is to check whether the pointer * in front of the function name is included in parentheses (). If it is included, it is a function pointer. Otherwise, it is a pointer function.

Let's talk about it in detail! See the following

Pointer function:
When a function declares that its return value is a pointer, it actually returns an address to the called function to be used in an expression that requires a pointer or address.
Format:
Type specifier * function name (parameter)
Of course, because an address is returned, the type specifiers are generally int.
Example: int * GetDate ();
Int * aaa (int, int );
The function returns an address value, which is often used on an element address of the returned array.

Int * GetDate (int wk, int dy );

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 ));
}

Int * GetDate (int wk, int dy)
{
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];
}
The program should be well understood. The subfunction returns the address of an element in the array. The output is the value in this address.

 

 

 

2. A function pointer is a pointer variable pointing to a function, which is essentially a pointer variable.

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

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

 

The pointer to a function contains the address of the function, which can be used to call the function. The Declaration format is as follows:
Type specifier (* function name) (parameter)
In fact, it cannot be called the function name. It should be called the variable name of the pointer. This special Pointer Points to a function that returns an integer value. The pointer statement is the same as the statement pointing to the function.
Parentheses outside the pointer and pointer operators change the default operator priority. Without parentheses, it becomes a prototype declaration of a function that returns an integer pointer.
For example:
Void (* fptr )();
Assign the function address to the function pointer in the following two forms:
Fptr = & Function;
Fptr = Function;
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 = (* fptr )();
X = fptr ();
The second format looks like a function call. However, some programmers tend to use the first format because it explicitly states that the function is called through pointers rather than function names. The following is an example:

Void (* funcp )();
Void FileFunc (), EditFunc ();

Main ()
{
Funcp = FileFunc;
(* Funcp )();
Funcp = EditFunc;
(* Funcp )();
}

Void FileFunc ()
{
Printf (FileFunc \ n );
}

Void EditFunc ()
{
Printf (EditFunc \ n );
}

Program output:
FileFunc
EditFunc

The main difference is that one is a pointer variable and the other is a function. It is necessary to make it clear before it can be used correctly.

 

2. pointer
The pointer seems confusing. Their declaration has two asterisks. For example:
Char ** cp;
If there are three asterisks, It is the pointer of the pointer, the four asterisks are the pointer of the pointer, and so on. When you are familiar with simple examples, you can cope with complex situations. Of course, in the actual program, only the second-level pointer is used. The three asterisks are not common, let alone the four asterisks.
The pointer must use the pointer address.
Char c = 'a ';
Char * p = & c;
Char ** cp = & p;
The pointer can not only access the pointer it points to, but also access the data pointed to by the pointer it points. The following are several examples:
Char * p1 = * cp;
Char c1 = ** cp;
You may want to know how this structure works. The pointer allows the called function to modify local pointer variables and process pointer arrays.

Void FindCredit (int **);

Main ()
{
Int vals [] = {, 5,-, 0 };
Int * fp = vals;
FindCredit (& fp );
Printf (% d \ n, * fp );
}

Void FindCredit (int ** fpp)
{
While (** fpp! = 0)
If (** fpp <0) break;
Else (* fpp) ++;
}

First, use an array address to initialize the pointer fp, and then pass the pointer address as a real parameter to the FindCredit () function (). The FindCredit () function indirectly obtains the data in the array through the expression ** fpp. To traverse the array to find a negative value, the FindCredit () function performs auto-increment operations on the object that the caller points to the array instead of its own pointer to the caller pointer. The statement (* fpp) ++ is used to perform auto-increment operations on the pointer pointed to by the form parameter pointer. However, because the * operator is higher than the ++ operator, parentheses are required here. If no parentheses exist, the ++ operator applies to the double pointer fpp.

3. pointer to the pointer Array
Another method of pointer is to process the pointer array. Some programmers prefer to use pointer arrays instead of multi-dimensional arrays. A common usage is to process strings.

Char * Names [] =
{
Bill,
Sam,
Jim,
Paul,
Charles,
0
};

Main ()
{
Char ** nm = Names;
While (* nm! = 0) printf (% s \ n, * nm ++ );
}

First, initialize the pointer nm with the address of the struct pointer array Names. Each call to printf () first passes the attention pointer pointed to by the pointer nm, and then carries out an auto-increment operation on the nm so that it points to the next element (or pointer) of the array ). Note that the preceding syntax is * nm ++. It first obtains the content pointed to by the pointer and then auto-increment the pointer.
Note that the last element in the array is initialized to 0, and the while loop is used to determine whether it is at the end of the array. Pointers with zero values are often used as terminator for loop arrays. The programmer calls the zero-value pointer a NULL pointer ). The NULL pointer is used as the Terminator. When adding or deleting elements to the tree type, you do not need to modify the code for traversing the array, because the array still ends with a null pointer.


Function pointers are different from pointer functions and how to apply them. examples show how to understand their concepts.

1. function pointer variable
In C language, a function always occupies a consecutive memory zone, and the function name is the first address of the memory zone occupied by the function. We can assign the first address (or entry address) of the function to a pointer variable so that the pointer variable points to the function. Then, the pointer variable can be used to locate and call this function. We call this pointer variable pointing to a function "function pointer variable ".
Function pointer variables are defined as follows:
Type specifier (* pointer variable name )();
The "type specifier" indicates the type of the return value of the specified function. "(* Pointer variable name)" indicates that the variable after "*" is the defined pointer variable. The final empty parentheses indicate that the pointer variable refers to a function.
For example:
Int (* pf )();
Pf is a pointer variable pointing to the function entry. The return value (function value) of this function is an integer.
[Example] This example describes how to use a pointer to call a function.
Int max (int a, int B ){
If (a> B) return;
Else return B;
}
Main (){
Int max (int a, int B );
Int (* pmax )();
Int x, y, z;
Pmax = max;
Printf ("input two numbers: \ n ");
Scanf ("% d", & x, & y );
Z = (* pmax) (x, y );
Printf ("maxmum = % d", z );
}

The procedure for calling a function in the form of a function pointer variable is as follows:
1) define the function pointer variable first, for example, the second line of int (* pmax) () in the next program, and define pmax as the function pointer variable.
2) Assign the entry address (function name) of the called function to the function pointer variable, for example, 11th rows of pmax = max in the program;
3) call a function in the form of a function pointer variable, such as the program's 14th line z = (* pmax) (x, y );
4) The general form of calling a function is:
(* Pointer variable name) (real parameter table)
Note the following when using function pointer variables:
A) function pointer variables cannot be used for arithmetic operations, which is different from array pointer variables. Adding or subtracting an integer from an array pointer variable can move the pointer to an array element behind or before it, and moving the function pointer is meaningless.
B) In a function call, the brackets on both sides of "(* pointer variable name)" are indispensable. * should not be considered as a value calculation, where * is only a symbol.

2 pointer Functions
As we mentioned earlier, the so-called function type refers to the type of the function return value. In C language, the return value of a function is a pointer (that is, an address). The function that returns the pointer value is called a pointer function.
The general form of defining pointer functions is:
Type specifier * function name (parameter table)
{
...... /* Function body */
}
"*" Is added before the function name, indicating that this is a pointer function, that is, the return value is a pointer. The type description indicates the Data Type pointed to by the returned pointer value.
For example:
Int * ap (int x, int y)
{
.../* Function body */
}
Ap is a pointer function that returns the pointer value. It returns the pointer to an integer variable.
[Example] This program uses a pointer function to input 1 ~ An integer between 7 and the name of the week.
Main (){
Int I;
Char * day_name (int n );
Printf ("input Day No: \ n ");
Scanf ("% d", & I );
If (I <0) exit (1 );
Prin ...... remaining full text>

The difference between the function pointer and the return value of the pointer Function

A pointer function is a function with a pointer. A function returns a pointer of a certain type.

Type identifier * function name (parameter table)

Int * f (x, y );

A function pointer is a pointer variable pointing to a function.

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

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

The main difference is that one is a pointer variable and the other is a function. It is necessary to make it clear before it can be used correctly.

I want you to use it.

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.