06-18-2010 05:39 PM
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)
{
}
Solved! Go to Solution.
06-19-2010 02:07 AM
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.
06-19-2010 12:08 PM
Hello frogonly -
You should change your ModuleTestP definition to look like the following:
NickB
National Instruments
06-21-2010 10:43 AM
Nick,
Thanks for pointing out the missing 'void'. It works.
Frogonly
06-21-2010 10:50 AM
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
06-22-2010 07:10 PM
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.