Again on function pointers, function pointers arrays

Source: Internet
Author: User
Tags define function


First, the basic concept
Array Name: The essence is a constant pointer to the first element of the array, that is, the first address of the array
Function Name: The essence is a constant pointer to the first instruction of the functions, that is, the first address
Function pointer: holds the first address of a function, which can be considered an alias


Second, the method of declaring function pointers
Type (*func) (type &, type &)
1, the statement declares a pointer func, which points to a function;
2, the function with 2 type parameters, and return a type of the value;
P.S. Tpye types can be considered as C + + types such as int or float
3, the whole function can be regarded as a data type
Tpye func (type, type)
Pointer variable func points to such a data type


Third, the matters needing attention
1, a pointer to a function must ensure that the function is defined, and is allocated memory,
Otherwise it points to an empty address, which is a big taboo!
2, pay special attention to the position of the first bracket, if the parentheses are removed
Type *func (type &, type &)
It becomes a function that returns type *.


Iv. application Examples of function pointers
1. Calling functions through function pointers
# include <stdio.h>
void f (int x)
{
printf ("%d\n", x);
}
int main ()
{
f (10);//Call function F directly
void (*p) (int);//define a function pointer p
p = f;//is assigned to the pointer variable p, the function name F is the first address of the function, and p=&f is correct
(*p) (20);//calling a function from a pointer rather than a function name
return 0;
}
2. typedef usage of function pointers
typedef void (*pint) (int);//declares that the function structure void f (int) is a data type
Pint is the name of the data type
//attention to typedef usage that differs from struct body
PINT p;
p = f;
3. function pointer as function parameter
Design a callmyfun function that can be called separately by the function pointer values in the arguments
MyFun1, MyFun2, MyFun3 These three functions (note: These three functions should have the same definition format).
# include <stdio.h>
typedef void (*PM) (int);//define a new function data type of PM
void Callmyfun (PM p, int x)//function pointer as formal parameter
{
p (x);
}
void MyFun1 (int x)
{
printf ("MyFun1 =%d\n", x);
}
void MyFun2 (int x)
{
printf ("MyFun2 =%d\n", x);
}
void MyFun3 (int x)
{
printf ("MyFun3 =%d\n", x);
}
int main ()
{
Callmyfun (MYFUN1, ten);
Callmyfun (MyFun2);
Callmyfun (MyFun3,);
return 0;
}


v. Further study
1, * (int*) &p----What is this?
void Function ()
{
printf ("Call function!\n");
}<br>
int main ()
{
void (*p) ();//define function pointer variable p
* (int*) &p= (int) function;//&p The address of the pointer variable p, which is a 32 bit binary number
//(int *) &p indicates that the address is coerced into a pointer to data of type int
//(int) function means to force the entry address of the functions into int type data
( *p) ();
return 0;
}
2, (* (Void (*) (void)) (0x30700000) ()-What is this?
void (*reset) (void) = (void (*) (void)) 0
reset ();
the left side of the equation defines the function pointer reset;
The right side of the equation is forced type conversion, and the value "0" is forcibly converted to the function pointer address "0";
the next sentence indicates that the call function reset () starts at address "0";
Example:
void (*theuboot) (void);//define function pointer Theuboot
theuboot = (void (*) (void)) (0x30700000);//absolute address 0x30700000
cast to the function pointer address and assign to Theuboot
theuboot ();//Call Function Theboot ()

P.S. The above code can be combined into the following line of code:
(* (Void (*) (void)) (0x30700000) ();
3. Summary
In fact, there is no difference between a function pointer and a normal pointer, only the content of the pointer is different.
The advantage of using a function pointer is that multiple modules that implement the same functionality can be identified together, which makes it easier to maintain later, and the system structure is clearer. Or summed up as: easy to layered design, conducive to system abstraction, reduce coupling and to separate the interface and implementation.

vi. array of function pointers
Example: char * (*pf[3]) (char *)
The function pointer array is essentially an array, where PF is the array name;
the array has 3 elements pf[0], pf[1], pf[2], respectively, storing 3 function pointers;
The return values of the 3 functions pointed to are char * and the parameters are char *;
Examples of programs:
# include <stdio.h>
void Fun1 (char * p)
{
printf ("%s\n", p);
}
void Fun2 (char * p)
{
printf ("%s\n", p);
}
void Fun3 (char * p)
{
printf ("%s\n", p);
}
int main ()
{
void (*pf[3]) (char *);//define function pointer array
pf[0] = fun1;
pf[1] = fun2;
pf[2] = fun3;
Pf[0] ("fun1");//parameter is a string "fun1", passed to the first address of the formal parameter string
Pf[0] ("fun2");
Pf[0] ("Fun3");
return 0;
}

pointer to array of function pointers
Example char * (* (*PF) [3]) (char * p);
the function pointer array pointer is essentially a pointer;
the pointer points to an array;
The array contains pointers to functions;
Examples of programs:
#include <stdio.h>
#include <string.h>

char * FUN1 (char * p)
{
printf ("%s\n", p);
return p;
}

char * fun2 (char * p)
{
printf ("%s\n", p);
return p;
}

char * FUN3 (char * p)
{
printf ("%s\n", p);
return p;
}

int main ()
{
char * (*a[3]) (char * p);
char * (* (*PF) [3]) (char * p);
PF = &a;

a[0] = fun1;
a[1] = &fun2;
a[2] = &fun3;

Pf[0][0] ("fun1");
pf[0][1] ("fun2");
pf[0][2] ("Fun3");
return 0;
}



Again on function pointers, function pointers arrays

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.