Typecasting issue

Posted by Kelvore on Sat 25 Nov 2006 05:20 AM — 2 posts, 11,717 views.

#0
Hello. I'm having a slight problem. I am writing a command editor and I was thinking to simplify the command name/do_function table by making interp.h serve two purposes.

#ifdef DECLARE_DO_FUN
#undef DECLARE_DO_FUN
#endif
#define DECLARE_DO_FUN(do_fun) ( {(char) do_fun, do_fun}, )
const struct cmdfun_type cmdfun_table [] =
{
#include "interp.h"
{"NULL",NULL}
};
#undef DECLARE_DO_FUN
#define DECLARE_DO_FUN(do_fun) void do_fun();

The basic idea is that it will load interp.h converting all the DECLARE_DO_FUN() statements into both a string for the function name ("do_advance") and the function name itself (do_advance). I keep getting this error, however:

interp.h:33: braced-group within expression allowed only inside a function
interp.h:33: `do_advance' undeclared here (not in a function)
interp.h:33: initializer element is not constant
interp.h:33: (near initialization for `cmdfun_table[0]')
interp.h:33: syntax error before ')' token

Any suggestions? I think the error is in my typecasting statement. I can't seem to find a good tip on the subject on the net, however.
USA #1
There are two problems. Firstly, you don't want the parentheses around your define body; remember that defines are inserted literally and in the array contents you don't want those parentheses.

Secondly, it doesn't make sense to typecast a function pointer to a character. (You'd also want a char*, not just a char, if you could do that in the first place.) You want the stringifier directive. If you enter #do_fun, then it will make a string out of the argument passed in, which is what you want to do.

To read more about this, just look through these:
http://www.google.com/search?&q=preprocessor%20stringify