| Back to logs list
3197060 2010 年 06 月 10 日 20:33 Reading (loading. ..) Comments (0) Category: Personal Diary
re-learn C
function pointer in the program is running, the function code is part of the program algorithm instructions, they are also occupied by the array as storage space,
mulberry handbag, has a corresponding address . You can use a pointer variable to point to the first address of the array, you can use a pointer variable to point to the first address of the function code, the code points to the first address of function pointer variable called function pointer.
1. Function pointers defined
function type (* pointer variable name) (parameter list);
function carried by the parameter list.
example:
int (* f) (int x);
double (* ptr) (double x);
in the definition of function pointers, please note:
function pointer and it points to the number and type of function parameters should be - consistent;
function pointer type and the return value type must also be consistent.
2. Function pointer assignment
function name and array name represents a function code as the first address, so the assignment, directly to the function pointer to function name on the line.
example,
int func (int x); / * declare a function * /
int (* f) (int x); / * declare a function pointer * /
f = func; / * will be the first address assigned to the func function pointer f * /
assignment function func with no brackets, no arguments, the function func on behalf of the first address, so after assignment after the pointer to point to the function f func (x) of the code, the first address.
3. Call the function through a function pointer
function pointer is the function name and the parameters of the call.
similar to other pointer variable, if the pointer variable pi is an integer variable i points to a pointer, then * p is equal to the variable it refers to i; if pf is a pointer to a pointer to float variable f, then * pf is equivalent to the variable it refers to f. Similarly,
burberry leather handbags, * f is a pointer to function func (x) of the pointer, then * f points to function on behalf of its func. Therefore, the implementation of the f = func; after, (* f) and func represent the same function.
function pointer as a function of storage area,
mulberry handbags, so you can call the appropriate function pointer function. Now we discuss how to use the function pointer to call the function, it should execute the following three steps:
First of all,
miu miu handbags, to explain the function pointer variable.
such as: int (* f) (int x);
Second, the function pointer variable.
such as: f = func; (func (x) must first have defined)
Finally, use (* pointer variable) (parameter list); call the function.
such as: (* f) (x); (x must be assigned)
number of arbitrary input n,
paul smith bags, find the largest number, and the maximum output value.
main ()
{
int f ();
int i, a, b;
int (* p) (); / * define function pointer * /
scanf (
p = f; / * p assign a function pointer to point to the function f * /
for (i = 1; i y)? x: y;
return (z);
}
operating results:
343 -45 4389 4235 1 -534 988 555 789?
pointer function
a function not only to bring back the value of an integer data, character type value and the value of type Real, you can also bring back the type of data pointer to point to an address unit.
function returns a pointer,
handbags uk, the general definition of the format:
type identifier * function name (parameter list)
int * f (x, y);
where x, y is the form parameter, f is the function name, call return a pointer to the integer address of the data pointer. f (x, y) is a function, its value is a pointer.
such as: char * ch (); that is a function return char pointer, see the following example:
string 1 (str1) copied to the string 2 (str2), and outputs a string 2.
# include
main ()
{
char * ch (char *, char *);
char str1 [] =
char str2 [] =
printf (
}
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);
}
by analyzing the available
function pointer is a pointer to a function, while the pointer function only shows that he is a pointer to a function return value,
function pointer can be used to point to a function.