From: utzoo!decvax!ucbvax!CAD:ucbesvax!turner
Newsgroups: net.unix-wizar
Title: cpp macro hack - RSVP - (nf)
Article-I.D.: ucbcad.694
Posted: Wed Feb 23 17:44:09 1983
Received: Thu Feb 24 20:57:34 1983

#N:ucbesvax:9600001:000:661
ucbesvax!turner    Feb 23 01:23:00 1983


/*
 * Does this work on your compiler?  The output should be as follows:

foo[0].name=hello, foo[0].val=1
foo[1].name=goodbye, foo[1].val=2

 * I would appreciate any and all responses to this question.  Basically,
 * I'm interested in the portability of these macro definitions.
 */
#define QUOTE(f) \
"\
f\
"

#define str_sym(sym) QUOTE(sym), &sym

int hello = 1,
    goodbye = 2;

struct
    {
    char *	name;
    int *	val;
    } foo [2] =
	{
	str_sym(hello),
	str_sym(goodbye)
	};

#define printv(x)\
    printf("%s.name=%s, %s.val=%d\n",\
	    QUOTE(x),\
	    x.name,\
	    QUOTE(x),\
	    *(x.val))

main()
{
    printv(foo[0]);
    printv(foo[1]);
}