A trick c question

User avatar
dima_ca
Уже с Приветом
Posts: 2226
Joined: 16 Jan 2004 22:05
Location: East Bay, CA

A trick c question

Post by dima_ca »

Say I have a set of defines like this:

Code: Select all

#define THIS_FANCY_DEFINE   1234
and want the program print

Code: Select all

"THIS_FANCY_DEFINE is 1234"
(one line per definition)

this is what I was trying to do:

Code: Select all

# define DESC(a) { "" ##a "", a }
typedef struct {
  const char* name;
  int         value;
} desc;

desc array[] = { DESC(THIS_FANCY_DEFINE) };
int
main()
{
   printf ( "%s: %d\n", array[0].name, aray[0].value );
}
unfortunately this does not compile properly, as the preprocessor replaces ##a with 1234 instead of replacing it with the THIS_FANCY_DEFINE string.

So, basically I need to supress macro expansion in one case. Does anyone know how to do this?
Безапелляционность - признак глупости.
User avatar
olg2002
Уже с Приветом
Posts: 990
Joined: 27 Mar 2002 10:01
Location: Palo Alto, CA

Post by olg2002 »

Code: Select all

# define DESC(a) {  #a, a } 
User avatar
dima_ca
Уже с Приветом
Posts: 2226
Joined: 16 Jan 2004 22:05
Location: East Bay, CA

Post by dima_ca »

olg2002 wrote:

Code: Select all

# define DESC(a) {  #a, a } 

you are the man (or the woman :lol: ), thanks!
Безапелляционность - признак глупости.

Return to “Вопросы и новости IT”