Go Down

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

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