Electronic – Function Pointer in C Language

operating systempic

I'm facing a little problem with function pointers in a code for the microcontroller Microchip Pic 18 Series.

The code below shows the prototypes of the functions involved in the problem and a call to the function 'insert_task', who should receive the address of the function 'task1' by the parameter 'task'.

However, I'm debugging the code in Proteus Isis and the parameter 'task' doesn't receive the address of the function passed as argument. More specifically, the parameter 'task' doesn't receive anything.

Anyone can see an error in the code?

void insert_task(uint8 priority, type_t type, void (*task)());
void task1();

insert_task(0, 0, task1);

The printscreen below shows the field "value" of the pointer "task" ("tarefa" in portuguese) without a value during the call of "insert_task" ("insere_tarefa" in portuguese).

debug

Best Answer

If you're using the XC8 compiler, it handles pointers to functions by creating a jump table in code memory. The actual function pointer is then an offset value that's used to index the jump table.

In a debugger, you'll never see the actual code-space address of the function in the pointer. In fact, if you happen to be passing a pointer to the first function in the table, the pointer value will correctly be zero.