Cypress Perform

Home > Support
support.cypress.com     Bookmark and Share
Support

Knowledge Base Article



Function Pointer - KBA84041

Last Updated: 12/07/2012

Question: How do I declare pointer to functions?

Answer:

Function pointer:
A function pointer is a variable that stores the address of a function that can later be called through that function pointer. By changing the value of a pointer we can call different functions in the same project. For this, the prototype of function pointer and function which is called using the pointer should be the same.


A function pointer can be declared as:


(*) (type of function arguments)


For example:


void (*fptr) (int);


Here, fptr is a pointer to a function taking one argument, an integer, and that returns void. To call the function pointed to by a function pointer, you treat the function pointer as though it were the name of the function you wish to call.


#include
void my_func1(int x)
{
 //print the function


void my_func2(int y)
{
    //print the function
}


void main()
{

 

void (*fptr) (int);


fptr=&my_func1;  //& is optional
(*fptr)(2);        //calling the function my_func1 through pointer


fptr=&my_func2;  //& is optional
(*fptr)(6);        //calling the function my_func2 through pointer


}

 Function pointer is the property of C. So all the microcontrollers, such as PSoC 1, PSoC 3, and PSoC 5 will support it.


Related Categories: PSoC® Software




Provide feedback on this item to help us improve:

How likely are you to recommend this article to a friend or colleague?

Not at all likely
0
1
2
3
4
5
6
7
8
9
10
Extremely likely

Was this item helpful?
Yes
No
Maybe


Additional comments:

Email:

Sunset Owner: KEAJ; Secondary Owner: MNSB; Sunset Date: 06/07/13