LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

C function pointer not accepted

Solved!
Go to solution

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 Kudos
Message 1 of 6
(3,801 Views)

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 Kudos
Message 2 of 6
(3,788 Views)
Solution
Accepted by topic author frogonly

Hello frogonly - 

 

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

 

typedef void (*ModuleTestP)(void);

 

NickB

National Instruments

0 Kudos
Message 3 of 6
(3,776 Views)

Nick,

 

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

 

Frogonly

0 Kudos
Message 4 of 6
(3,721 Views)

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. Smiley Surprised But nothing wrong about that.

 

frogonly

0 Kudos
Message 5 of 6
(3,719 Views)

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 Kudos
Message 6 of 6
(3,682 Views)