You are here

Function Pointer - KBA84041 | Cypress Semiconductor

Support & Community

Function Pointer - KBA84041

Last Updated: December 06, 2015

How do I declare pointer to functions?


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:
<return type of function> (*<name of pointer>) (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. 

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, PSoC4 and PSoC 5 will support it.

Knowledge Base Tags: 

Provide feedback on this article

Browse KB By Product

Browse KB by Type