Pointers to Program Memory and Functions | Cypress Semiconductor
Support & Community
Pointers to Program Memory and Functions
Last Updated: January 12, 2010
Question: How do I declare Pointers to Program Memory and pointers to functions?
Response: The qualifier “const” is used to make the pointer point to program memory. But the position where the qualifier “const” is put will give different meaning to pointer. The position decides whether the const applies to pointer variable itself or the data, to which it is pointing. See examples below,
const char *ptr1;
Here the ptr1 is pointer stored in data memory and points to char data in program memory.
char * const ptr2;
Here the ptr2 is stored in program memory and points to char data in data memory.
const char * const ptr3;
Here the ptr3 is stored in program memory and points to data in program memory.
The declaration of the function pointers and calling a function using pointer is shown below.
void func(void); // Function
void (*fptr)(void); // Declare a Function pointer
fptr = func; // Asigning the value of function pointer to the pointer
(*fptr)(); // Function call through pointer
If this pointer is to be stored in program memory then,
void (*const fptr)(void) = func; // Function pointer