Re: Cannot understand the following codes



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



Relevant Pages

  • Re: Passing execution to a memory address
    ... forbidden initialization of function pointer with void pointer ... int add1 ...
    (comp.lang.c)
  • Re: function * = void *
    ... It doesn't have to be the *correct* kind of function pointer: ... It's prototyped to return a void *. ... The ISO C standard does not require that pointers to functions can be ...
    (comp.lang.c)
  • Re: Some pointer quiestions again
    ... A function pointer can be converted to a void pointer but ... You can do the conversion with a cast: ... >void pointer reliably, if not then there would appear to be no portable ...
    (comp.lang.c)
  • Re: function * = void *
    ... a.c:14: warning: ISO C forbids conversion of object pointer to function ... There is no guarantee that a function pointer can FIT in a void * ... It wouldn't surprise me if there will be new platforms where there ...
    (comp.lang.c)
  • Re: man malloc
    ... % foo.c:14: warning: ISO C forbids conversion of function pointer to object pointer type ...
    (freebsd-questions)