Go Down

Topic: Doubt about this C code... (Read 1 time) previous topic - next topic

Igor R

Hi,

I´m looking into create a vector of functions. Like used in http://www.arduino.cc/playground/Main/PcInt

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

volatile static voidFuncPtr PCintFunc[24] = {
 NULL };



It's great this programmation trick to use after like PCintFunc[pin]();
It makes easier lcd menu functions or flow control into interrupts.

Thank you very much!!  ;)



Igor R.

halley

I would leave out the [glow]volatile[/glow] and [glow]= { NULL }[/glow] 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.

Igor R

Hi Halley,

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


Thank-u


Regards,



Igor R.

halley

http://wiki.answers.com/Q/What_is_[glow]typedef[/glow]_in_C

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.

Igor R

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 => http://www.newty.de/fpt/index.html

Regards,


Igor R.  ;)

westfw

Quote
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:
Code: [Select]
typedef struct foo foo_type;
It would make more sense as something like:
Code: [Select]
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"

Igor R

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!!




mem

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.

Igor R

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



;)

dcb

#9
Dec 30, 2008, 02:08 am Last Edit: Dec 30, 2008, 03:36 am by dcb Reason: 1
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?" ;) .  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).

Go Up