Doubt about this C code...


I´m looking into create a vector of functions. Like used in Arduino Playground - PcInt

But I´m not understand the code:
typedef void (*voidFuncPtr)(void);

volatile static voidFuncPtr PCintFunc[24] = {

It's great this programmation trick to use after like PCintFuncpin;
It makes easier lcd menu functions or flow control into interrupts.

Thank you very much!! :wink:

Igor R.

I would leave out the volatile and = { NULL } parts, but otherwise seems fine to me.

Unless the hardware itself or an interrupt routine is changing a variable, AND the variable is used multiple times in the same routine, you don't need the volatile keyword. A function pointer is rarely used multiple times, and I doubt you're modifying the table from an interrupt service routine.

The initializer is optional and should have the same number of entries as the array declaration. You have one NULL but 24 elements. All of them will be initialized as NULL anyway. And if you include an initializer, it just eats up RAM space anyway.

Hi Halley,

Could you explain me a little the line: typedef void (*voidFuncPtr)(void); ? :wink:



Igor R.

The specific typedef here is making a new type, voidFuncPtr, which is equivalent to a pointer to a function taking no arguments and returning no value.

Thank-u Halley!!

I didn´t know the function pointers before.

dcb (who use it in mpguino) has told me a link to understand it => The Function Pointer Tutorials - Index


Igor R. :wink:

Could you explain me a little the line: typedef void (*voidFuncPtr)(void);

It looks particularly weird because C typedefs look like variable declarations, and pointers to functions have the name ("voidFuncPtr", here) in the middle of a bunch of punctuation instead of off as a nice separated token the way it would be for a more common type:

typedef struct foo foo_type;

It would make more sense as something like:

typedef void (*)(void) voidFuncPtr;

but that's not the way C works. Read it as:
"define a type voidFuncPtr as a pointer to a function with no arguments that returns void"

Thank-u Westfw!!

After a little search on the net about Function Pointers,now it is clear (more or less to be pointers.... :D).

It is very interesting!!! I have a switch case in my applications to control the flow of LCD menu (diferent functions calls), but it´s better with this!!! Powerful!!

Igor, why do you think its better?

Function pointers are powerful, but they are more confusing to read and understand than switch statements. Unless you really need that extra power, IMO it best to keep things nice and simple by sticking to switch statements.

Yes, of course. I´m agree that they are more confusing....

I´m going to tinker with it... maybe is the emotion of the new knwoledge!!! ;D


I used function pointers mainly as an organizational tool. I like looking at concise lists of things, i.e. available display screens or events, without wading through the logic of a switch i.e. "oops, was there an extra semicolon?" :wink: . I don't know that it is better or worse than a switch in general though, matter of taste IMHO.

With one or two functions a switch is probably better, but if you have enough to call it a list then it is ok to consider putting them in a list (array).