Information from the Internet:
Pointer function: A function that returns a pointer to a value
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.
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.
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.But what is the purpose of the return address??
int * GetDate (int wk,int dy);
Main ()//I don't understand, it's not pseudo-code.
{
int wk,dy;
Do
{
printf (Enter Week (1-5) day (1-7) \ n);
scanf (%d%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 child function returns the address of an element of an array. The output is the value in this address.
Second, function pointer: Refers to a pointer to a function variable , essentially a pointer variable .
Int (*f) (int x); Declares a pointer variable/* Declares a function pointer */
F=func; F this pointer variable points to the Func function/* 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)//the use of this parameter is not seen Ah, this is the return value
This is not actually called a function name,A variable name that should be called a pointer. This particular pointer points to a function that returns an integer value。 The declaration of a pointer must be consistent with the declaration 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.
For example:
void (*fptr) ();
assigning the address of a function to a function pointer can take the following two forms:
fptr=&function;
fptr=function;
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) ();
x=fptr ();
The second format looks like a function call. But some programmers tend to use the first format because it clearly states that it isthrough pointers instead of function namesTo invoke the function. Here's an example:
void (*FUNCP) ();
void Filefunc (), Editfunc ();
Main ()
{
Funcp=filefunc; The function pointer funcp points to the function Filefunc
(*FUNCP) (); Call the function that FUNCP points to Filefunc
Funcp=editfunc; The function pointer funcp again to editfunc this function
(*FUNCP) (); Call the function that FUNCP points to Editfunc
}
void Filefunc ()
{
printf (filefunc\n);
}
void Editfunc ()
{
printf (editfunc\n);
}
The program output is:
Filefunc
Editfunc
function pointers and pointer functions, 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 ';
Char *p=&c;
Char **cp=&p;
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:
Char *P1=*CP;
Char C1=**CP;
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 * *);
Main ()
{
int vals[]={7,6,5,-4,3,2,1,0};
int *fp=vals;
Findcredit (&FP);
printf (%D\N,*FP);
}
void Findcredit (int * * FPP)
{
while (**fpp!=0)
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.
Char *names[]=
{
Bill,
Sam,
Jim,
Paul,
Charles,
0
};
Main ()
{
Char **nm=names;
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.
Programming one by one C language problems, pointer functions and function pointers