Pages: [1]   Go Down
Author Topic: Doubt about this C code...  (Read 956 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Edison Member
*
Karma: 16
Posts: 1579
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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



Igor R.
Logged


Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Edison Member
*
Karma: 16
Posts: 1579
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Halley,

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


Thank-u


Regards,



Igor R.
Logged


Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Edison Member
*
Karma: 16
Posts: 1579
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.  smiley-wink
Logged


SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6652
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Edison Member
*
Karma: 16
Posts: 1579
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank-u Westfw!!

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

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



Logged


London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Edison Member
*
Karma: 16
Posts: 1579
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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



 smiley-wink
Logged


0
Offline Offline
God Member
*****
Karma: 1
Posts: 513
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?" smiley-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).
« Last Edit: December 29, 2008, 09:36:26 pm by dcb » Logged

Pages: [1]   Go Up
Jump to: