LabWindow/CVI's C compiler does not seem to work on function pointer. The following simplified code generate compilation errors "missing prototype" at (*ModuleTest)() line and function declaration of FindModuleTest. The same code with cvi inclues removed has no problem under MS visual C.
#include <stdio.h>
#include <ansi_c.h>
#include <cvidef.h>
#include <windows.h>
#include <cvirte.h>
void fa(void);
void fb(void);
void fc(void);
void fd(void);
typedef void (*ModuleTestP)();
struct { char *name; ModuleTestP funcptr;} symtab[] = {
"fa", fa,
"fb", fb,
"fc", fc,
"fd", fd
};
ModuleTestP ModuleTest;
ModuleTestP FindModuleTest(char *name);
int main( int argc, char **argv )
{
ModuleTest = FindModuleTest("fa");
(*ModuleTest)();
}
ModuleTestP FindModuleTest(char *name)
{
int i;
for (i=0; i<sizeof(symtab)/sizeof(symtab[0]); i++)
{
if (strcmp(name, symtab[i].name) == 0) return symtab[i].funcptr;
}
return NULL;
}
void fa(void)
{
}
void fb(void)
{
}
void fc(void)
{
}
void fd(void)
{
}
已解决! 转到解答。
CVI works nicely with function pointers.
You might try two options: provide a prototype, or uncheck (in Options/Build Options) the 'require function prototype' option.
Wolfgang,
Thanks for help. I did have a typedef except that in the typedef I miss the explicit 'void' in the parameter. By adding the 'void', it build successfully. I guess CVI compiler is less forgiving than the MS Visual C.
But nothing wrong about that.
frogonly
A lot of programmers treat the void declaration in the parameter list as an optional thing but it really should be explicitly stated in the function prototype. The fact that Microsoft allows it to be omitted is not a good practice.
This is one of those practices in programming that goes along with explicitly initializing variables to save a lot of headache and confusion.