Re: Cannot understand the following codes
- From: Ben C <spamspam@xxxxxxxxx>
- Date: 28 Mar 2006 22:13:09 GMT
On 2006-03-28, gpsabove@xxxxxxxxx <gpsabove@xxxxxxxxx> wrote:
I got problems on understanding the attached codes. It seems a 3rd
party API library is called.
Question1: Why assign NULL to GO_FUNCS?
Probably just so you can check for it later-- at some time stGOFuncs
presumably gets filled in with real pointers to functions. If you
initialize them to NULL you can assume that if they're not NULL at some
later point that they've been assigned something useful.
Question2: GO_FUNCS looks like a struture of functions. Is there a
term for it?
It could be an array rather than a structure. I know of know special
name for it. Sometimes people call this kind of thing a "vtable" if it's
used in a particular way.
Question 3: I cannot understand this line in total.
(*(void (*) (int,t_go_measurements[]))(stGOFuncs.p_go_Measurements))(i_,ast_);
Why parenthesize an integer and a structure together?
There was a recent posting on comp.lang.c with the subject "Re: typedef
int (*MyFunctionP)();" with a very good explanation of these.
This:
(void (*) (int,t_go_measurements[]))
is a cast. The type it's casting to is "pointer to function that returns
void and takes two arguments, an int and an array of t_go_measurements".
The code casts stGOFuncs.p_go_Measurements to this function pointer
type, and then calls it, with i_ and ast_.
Syntax for calling a function pointer is:
(*fn)(args);
So this could have been written somewhat more legibly like this:
typedef void (*fn_t)(int, t_go_measurements[]);
(*(fn_t) (stGOFuncs.p_go_Measurements))(i_, ast_);
Or even more legibly still:
typedef void (*fn_t)(int, t_go_measurements[]);
fn_t fn = (fn_t) stGOFuncs.p_go_Measurements;
(*fn)(i_, ast_);
What is possibly the type of stGOFuncs.p_go_Measurements? I did not
see its definition.
Some sort of pointer type presumably.
I know these questions might seem silly to you, but please help. I
also want to get some links for this type of programming.
FWIW I wouldn't particularly recommend this style of programming! For
one thing appears to be C++, which means there's quite likely to be a
better way of achieving the same result using either classes or
functors.
.
- References:
- Cannot understand the following codes
- From: gpsabove
- Cannot understand the following codes
- Prev by Date: Re: Cannot understand the following codes
- Next by Date: Lex
- Previous by thread: Re: Cannot understand the following codes
- Next by thread: Re: Cannot understand the following codes
- Index(es):
Relevant Pages
|