I'd like to shorten-up so redundant code in a switch statement by using
a macro for each case. The following code illustrates the problem. In
short, is there a way to write the PARSE(a,b) macro that will work in
the manner shown below?
Thanks,
H.
========
#include <stdio.h>
int varAB = 1;
int varFG = 2;
#define TEST1(a,b) printf("'%c' '%c' = %d\n", *#a, *#b, var##a##b)
#define TEST2(a,b) printf("'%c' '%c' = %d\n", #a[0], #b[0], var##a##b)
int ParseAB(void) { return(0); }
int ParseCD(void) { return(0); }
int ParseEF(void) { return(0); }
// * not allowed in case statement
#define PARSE(a,b) case (*#a << 8) | *#b: \
status = Parse##a##b(); \
break
// ends up being literally 'a' and 'b'
#define PARSE2(a,b) case ('a' << 8) | 'b': \
status = Parse##a##b(); \
break
int main()
{
int status = 0;
TEST1(A,B);
TEST2(F,G);
switch (('A' << 8) | 'B')
{
// This is what the output of the PARSE macro should be:
case ('A' << 8) | 'B':
status = ParseAB();
break;
// This is how I'd like to use the macro.
PARSE(C,D);
PARSE(E,F);
default:
status = -1;
}
return(0);
}