LabWindows/CVI

取消
显示结果 
搜索替代 
您的意思是: 

C function pointer not accepted

已解决!
转到解答

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)
{
   
}

0 项奖励
1 条消息(共 6 条)
4,645 次查看

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.

0 项奖励
2 条消息(共 6 条)
4,632 次查看
解答
接受人 frogonly

Hello frogonly - 

 

You should change your ModuleTestP definition to look like the following:

 

typedef void (*ModuleTestP)(void);

 

NickB

National Instruments

0 项奖励
3 条消息(共 6 条)
4,620 次查看

Nick,

 

Thanks for pointing out the missing 'void'. It works.

 

Frogonly

0 项奖励
4 条消息(共 6 条)
4,565 次查看

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

0 项奖励
5 条消息(共 6 条)
4,563 次查看

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.

Martin Fredrickson
Test Engineer

Northrop Grumman
Advanced Systems and Products
San Diego, CA 92128
0 项奖励
6 条消息(共 6 条)
4,526 次查看