Re: Cannot understand the following codes



* gpsabove@xxxxxxxxx:
I got problems on understanding the attached codes. It seems a 3rd
party API library is called.

Question1: Why assign NULL to GO_FUNCS?

Those NULLs are initial values.


Question2: GO_FUNCS looks like a struture of functions. Is there a
term for it?

"Bad code".


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?

void (*)(int,t_go_measurements[])

is a the type of a pointer to a function that takes and int and a pointer to a t_go_measurements as arguments, and returns void.

You can see that more easily if you make it a typedef:

typedef void (*FuncPointerType)(int,t_go_measurements[])

Starting with something x that is of FuncPointerType, you first apply dereferencing, *x, which gives you something that you can add an argument list with an int and a t_go_measurements[] in, which produces void; from that you can infer the type of x, what FuncPointerType is.

The line casts stGoFuncs.p_go_Measurements to that function pointer type, and then calls the function with arguments i_ and ast_.


What is possibly the type of stGOFuncs.p_go_Measurements? I did not see
its definition.

It's probably a void(*)(). But not matter what type it has, the cast is undefined behavior in C++, and I believe also in C.


Why put void (*) ahead of the line?

See above.


I know these questions might seem silly to you, but please help. I also
want to get some links for this type of programming.

You really don't want to use that type of Undefined Behavior low-level programming.


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "goapi.hpp"

This indicates the C++, but the code isn't even at the level of C.


GO_FUNCS stGOFuncs =
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};

void
go_Measurements(int i_, t_go_measurements ast_[])
{
(*(void (*)
(int,t_go_measurements[]))(stGOFuncs.p_go_Measurements))(i_,ast_);
}


//Find the definition of t_go_measurements in the header file
/*
* This structure holds a set of measurements
*/
typedef struct {
long lSpeed;
int iUnit;
...
double dDistance;
} t_go_measurements;



--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.



Relevant Pages

  • Re: Can I send char as array argument?
    ... Everything about programming I have learned ... > void main ... to it with a pointer. ... Your solution was to declare a 1-element array of char ...
    (comp.lang.c)
  • Re: C programming on ARM
    ... bitpattern, but after that there are no limits. ... And often when programming, you know exactly what compiler and target you are using, and can program accordingly rather than writing generic code. ... Aside from alignment problems (which may mean that using the pointer does not "just work", even if conversions back and forth do), there are many architectures with different pointer types and different memory spaces. ... You can't rely on these things having a consistent pointer size - you should not even rely on the C requirement of a "void *" supporting them all. ...
    (comp.arch.embedded)
  • &array considered harmful?
    ... The kernel has lots of code that takes the address of a local array. ... void func ... then A also behaves like a pointer and the code is broken. ... I can generate a patch to eliminate this programming ...
    (Linux-Kernel)
  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)
  • Re: [RFC] timers, pointers to functions and type safety
    ... * they have callback of type void ... callback is called by the code that even in theory has no ... cast to unsigned long and cast back in the callback. ... number - not a pointer cast to unsigned long, not an index in array, etc. ...
    (Linux-Kernel)