Re: Cannot understand the following codes
- From: "Alf P. Steinbach" <alfps@xxxxxxxx>
- Date: Wed, 29 Mar 2006 00:04:55 +0200
* 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?
.
- Follow-Ups:
- Re: Cannot understand the following codes
- From: Ben C
- Re: Cannot understand the following codes
- References:
- Cannot understand the following codes
- From: gpsabove
- Cannot understand the following codes
- Prev by Date: Re: Opinions on a good choice for application re-write
- Next by Date: Re: Cannot understand the following codes
- Previous by thread: Cannot understand the following codes
- Next by thread: Re: Cannot understand the following codes
- Index(es):
Relevant Pages
|