Transferred from: http://www.cnblogs.com/gmh915/archive/2010/06/11/1756067.html
This "pointer function" and "function pointer" are easy to mistake in learning arm, so today, I want to make it clear at once and find some information, first of all, the definition between them:
1, pointer function refers to a function with a pointer, that is, the essence is a function. A function return type is a pointer to a type
Type identifier * Function name (parameter table)
int *f (x, y);
First it is a function, except that the return value of this function is an address value. The function return value must be accepted with a pointer variable of the same type, that is, the pointer function must have a function return value, and in the keynote function, the function return value must be assigned to a pointer variable of the same type.
float *fun ();
p = Fun (a);
Note that the pointer function is different from the function pointer representation method, so don't confuse it. The simplest way to discern is to see if the pointer in front of the function name is enclosed in parentheses (), and if it is included as a function pointer, the inverse is a pointer function.
Tell me more about it! Take a look below
when a function declares its return value as a pointer, it actually returns an address to the calling function for use in an expression that requires a pointer or address.
Type Descriptor * Function name (parameter)
Of course, because an address is returned, the type specifier is generally int.
For example:int *getdate ();
int * AAA (Int,int);
The function returns an address value that is often used on the address of an element that returns an array.
int * GetDate (int wk,int dy);
printf (Enter Week (1-5) day (1-7) \ n);
while (wk<1| | wk>5| | dy<1| | DY>7);
printf (%d\n,*getdate (wk,dy));
int * GetDate (int wk,int dy)
static int calendar=
The program should be well understood, and the child function returns the address of an element of the array. The output is the value in this address.
2, the function pointer is a pointer to a function variable, that is, the essence is a pointer variable.
Int (*f) (int x); /* Declare a function pointer */
F=func; /* Assigns the first address of the Func function to the pointer f */
A pointer to a function contains the address of the function, which can be used to invoke the function. The declaration format is as follows:
Type Descriptor (* Function name) (parameter)
In fact, this is not called the function name, it should be called the pointer variable name. This special pointer points to a function that returns an integer value. The declaration of the pointer is consistent with the declaration that it points to the function.
The parentheses outside the pointer name and pointer operator change the default operator precedence. Without parentheses, it becomes a prototype declaration of a function that returns an integer pointer.
void (*fptr) ();
assigning the address of a function to a function pointer can take the following two forms:
the Fetch address operator & is not required because a function identifier alone represents its address, and if it is a function call, it must also contain a parameter table enclosed in parentheses.
There are two ways to invoke a function with pointers:
X= (*fptr) ();
The second format looks like a function call. However, some programmers prefer to use the first format because it clearly indicates that a function is called by a pointer rather than a function name. Here's an example:
void (*FUNCP) ();
void Filefunc (), Editfunc ();
void Filefunc ()
void Editfunc ()
The program output is:
The main difference is that one is a pointer variable and one is a function. In use is necessary to be clear to use correctly
Ii. pointers to pointers
The pointer's pointer looks somewhat confusing. Their declarations have a two asterisk. For example:
Char * * CP;
If there are three asterisks, that is a pointer to the pointer pointer, four asterisks is a pointer to the pointer pointer, and so on. When you are familiar with simple examples, you can deal with complex situations. Of course, in the actual program, it usually only uses a level two pointer, three asterisks are uncommon, let alone four asterisks.
The pointer's pointer needs to use the pointer's address.
Char c= ' A ';
A pointer to a pointer can access not only the pointer it points to, but also the data it points to. Here are a few examples:
You may wonder what the use of such a structure is. Pointers can be used to allow the called function to modify local pointer variables and manipulate array of pointers.
void Findcredit (int * *);
void Findcredit (int * * FPP)
if (**fpp<0) break;
Else (*FPP) + +;
first, the pointer FP is initialized with the address of an array, and the address of the pointer is passed as an argument to the function Findcredit (). The Findcredit () function indirectly obtains the data in the array through an expression **FPP. To iterate through the array to find a negative value, the object that the Findcredit () function is self-increment is the caller's pointer to the array, not its own pointer to the caller's pointer. The statement (*FPP) + + is the self-increment of the pointer to which the parameter pointer is pointing. However, because the * operator is higher than the + + operator, the parentheses are required here, and if there are no parentheses, the + + operator will be used on the double pointer FPP.
pointers to pointer arrays
Pointer to a pointer to another usage array of old processing pointers. Some programmers prefer to use pointer arrays instead of multidimensional arrays, and a common use is to manipulate strings.
while (*nm!=0) printf (%s\n,*nm++);
first, the pointer nm is initialized with the address of the names array of character pointers. Each call to printf () first passes the character pointer pointed to by the pointer nm, and then the NM is self-increment to point to the next element (or pointer) of the array. Note that the syntax described above is *nm++, which first obtains what the pointer points to and then makes the pointer increment.
Note that the last element in the array is initialized to the 0,while loop to determine if it is at the end of the array. Pointers with a value of 0 are often used as terminators for loop arrays. The programmer calls the 0-value pointer a null pointer (NULL). Using a null pointer as the terminator, when the tree species additions and deletions, it is not necessary to change the code traversing the array, because the array still ends with a null pointer.
The difference between a pointer function and a function pointer