Q: using union of function ptrs in MSVC 
Author Message
 Q: using union of function ptrs in MSVC

I am declaring a structure containing a union of pointers to functions
which take different arguments.  Then I initialize an array of these
structures.  Later I declare a pointer to this type of struct and it
ends up pointing at one of the array elements.  So I will ultimately
call the function via the pointer, and will be able to pass different
argument lists.

MSVC 4.2 complains with error 2440: 'initializing' when I declare the
array...seems like it wants my function to look like the first function
type declared in the union.  How can I tell the compiler that any of the
function types in my union are valid for this pointer?

Here's the code:

struct fentry {
  char *pszName;
  union {
    void (*pfv)();
    void (*pfc)(char *);
    void (*pfcc)(char *,char *);
    void (*pfccc)(char *,char *,char *);
    } pf;
  int  nArgs;
  };

fentry ftable[] = {
  { "cd",  (void (*)(char *))DoChDir,1 },             // compiler error 2440:
  { "get", (void (*)(char *))DoGet,  1 },             //  and here
  { "mode",(void (*)(char *))DoMode, 1 },             //  and here
  { "open",(void (*)(char *,char *,char *))DoOpen, 3 }, //  and here
  { "put", (void (*)(char *))DoPut,  1 },             //  and here
  { 0, 0, 0 }
  };

Any suggestions greatly appreciated.

Frank Brown

http://www.*-*-*.com/



Fri, 16 Apr 1999 03:00:00 GMT  
 Q: using union of function ptrs in MSVC

Quote:

>I am declaring a structure containing a union of pointers to functions
>which take different arguments.  Then I initialize an array of these
>structures.  Later I declare a pointer to this type of struct and it
>ends up pointing at one of the array elements.  So I will ultimately
>call the function via the pointer, and will be able to pass different
>argument lists.

>MSVC 4.2 complains with error 2440: 'initializing' when I declare the
>array...seems like it wants my function to look like the first function
>type declared in the union.  How can I tell the compiler that any of the
>function types in my union are valid for this pointer?

>Here's the code:

>struct fentry {
>  char *pszName;
>  union {
>    void (*pfv)();
>    void (*pfc)(char *);
>    void (*pfcc)(char *,char *);
>    void (*pfccc)(char *,char *,char *);
>    } pf;
>  int  nArgs;
>  };

>fentry ftable[] = {
>  { "cd",  (void (*)(char *))DoChDir,1 },         // compiler error 2440:
>  { "get", (void (*)(char *))DoGet,  1 },         //  and here
>  { "mode",(void (*)(char *))DoMode, 1 },         //  and here
>  { "open",(void (*)(char *,char *,char *))DoOpen, 3 }, //  and here
>  { "put", (void (*)(char *))DoPut,  1 },         //  and here
>  { 0, 0, 0 }
>  };

>Any suggestions greatly appreciated.

You are missing the keyword `struct' before `fentry ftable[]'; i.e;

struct fentry ftable[] = {
..

What are you casting here?

        (void (*)(char *))DoChDir

Your compiler is complaining, in part, because it doesn't know what DoChDir is
and the same goes for the other functions.

Therefore you need to have the prototypes and the actual functions for
DoChDir, DoGet etc:

void DoChDir(char *);
void DoGet(char *);
void DoMode(char *);
void DoOpen(char *,char *, char *);
void DoPut(char*);

If these are the correct function prototypes, then you do not
need to cast:

struct fentry ftable[] = {
  { "cd",  {DoChDir}, 1 },
  { "get", {DoGet},  1 },
  { "mode",{DoMode}, 1 },
  { "open",{DoOpen}, 3 },
  { "put", {DoPut},  1 },
  { 0, 0, 0 }

Rules for union initialisation:

The initializer must be a brace-enclosed constant expression, which is
allowable for the first member of the union. Note, C permits
the inner braces to be dropped in this instance. All the
above prototypes are compatible with:

        void (*pfv)();

--
------------------------------------------------------------------
Ed Breen
CSIRO, DMS                        Phone:+61 2 325 3208            
Locked Bag 17, Nth Ryde           Fax:+61 2 325 3200              

Building E6B                      URL:http://www.dms.csiro.au/~edb
Macquarie University Campus
------------------------------------------------------------------



Sat, 17 Apr 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Uses of union, function pointer etc.

2. array ptrs vs ptrs

3. near ptrs, far ptrs, Arghh!

4. Using MSVC 5.0 and MSVC 6.0 On One Machine

5. Qs on Function Call on Array

6. typedef of ptrs to functions

7. Typedef of ptrs to functions

8. Question re: storing ptrs to functions-that-return-float in structs

9. Casting function ptrs

10. table of function ptrs SOLVED

11. More on function ptrs

12. Function Ptrs, typedefs & protos

 

 
Powered by phpBB® Forum Software