# Function Pointer in C Language

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)());



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).

• I don't remember, but... I think you must specify void as the argument type of task pointer, i.e. void (*task)(void), if it doesn't take arguments. – Martin Petrei Sep 24 '14 at 12:19
• No. Didn't work, but thank you. – user49894 Sep 24 '14 at 12:23
• This question belongs on StackOverflow – Scott Seidman Sep 24 '14 at 12:28
• @ScottSeidman: No, it doesn't. It actually relates to the specifics of programming in C on microcontrollers that have a Harvard architecture (separate memory spaces for instructions and data). Support for things like function pointers (if it exists at all) is going to be very dependent on the specific compiler being used. – Dave Tweed Sep 24 '14 at 12:34
• @DaveTweed - If this counts as a hardware-specific problem because of the architecture then you probably need to migrate 75% of the contents of StackOverflow over to EE. It looks like pretty universal C code to me, should compile & run (when fixed, natch) on just about anything. – John U Sep 24 '14 at 13:26

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.

• I'm using C18. The same is valid to it? – user49894 Sep 24 '14 at 13:21
• I don't know. A quick Google didn't turn up anything that gave any hints about how it implements function pointers internally. But it does seem to have support for them. – Dave Tweed Sep 24 '14 at 13:48

The only error in your code is this line: void task1();

This is only function prototype. You need to write function defination.

Replace this line with following code :

void task1()
{
/* Write some code here */
Nop();//No operation
}


Just writing function prototype will not allocate memory.

• No, no, no. Here I just write the prototype, but the definition is in the code. – user49894 Sep 24 '14 at 13:19
• you can use PICKIT 3 or similar debugger with MPLAB to debug. Add task1 to watch or you can see deassembly code. – GOKU Sep 24 '14 at 13:27

I think the problem is a bug in Proteus. I tried to debug the code in MPLAB X and there it works. Thanks for your help!

• Good catch. Many compiler-specific PIC sites will simply turn you away if you haven't tried it on the chip for such reasons. – Scott Seidman Sep 24 '14 at 20:21

Also beware that many uC compilers have optimization settings that cause problems with function pointers. The optimization routines apparently can't "see" that some code is actually used and it gets optimized away. If you have sufficient code space, I recommend turning off or turning down the optimization level until you've finished debugging, and then increase the optimization and see if it still works.