0

I am familiar with std #defines and macro expansion for min max function but this one throws me. In particular the #'s in the definition. Your thoughts appreciated.

#define TAB_WIDGET_ROW(t)     {t##_seq, t##_len, t##_rep}
dbush
  • 205,898
  • 23
  • 218
  • 273
user2579721
  • 79
  • 1
  • 8
  • It's called token-pasting. Besides the gcc links that your have as answers, this one is from Visual Studio: https://msdn.microsoft.com/en-us/library/09dwwt6y.aspx – Happy Green Kid Naps Jul 29 '16 at 15:58

2 Answers2

2

You should read some documentation concerning the C Preprocessor. The ## is about Concatenation. So an invocation like TAB_WIDGET_ROW(xy) of your TAB_WIDGET_ROW macro would probably be expanded to {xy_seq, xy_len, xy_rep}

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

The ## is a C preprocessor standard macro used for concatenation.

This way, the code :

#define TAB_WIDGET_ROW(t)     {t##_seq, t##_len, t##_rep}

int foo[3] = TAB_WIDGET_ROW(bar);

Will expand to :

int foo[3] = {bar_seq, bar_len, bar_rep};

And the foo array will be filled with values of variables bar_seq, bar_len and bar_rep.

See here for more informations.

Quentin
  • 724
  • 7
  • 16
  • Thank you both. Good explanations and good links too. – user2579721 Jul 29 '16 at 16:11
  • I am nitpicking, but: `##` is *not* GCC specific. Any C99 or C11 (or C++11 etc...) compiler should understand it likewise. – Basile Starynkevitch Jul 29 '16 at 17:58
  • @BasileStarynkevitch If we're nitpicking, then the compiler doesn't care; it's the preprocessor that does that. ;-) But yup... the idea that this is GCC-specific is not actually a nit at all, just a dangerously false statement. This is totally Standardised. I'd ask that this answer be fixed to reflect that. Besides, the OP didn't mention GCC, so if you though this was GCC-specific, the answer wouldn't necessarily be relevant. – underscore_d Jul 29 '16 at 22:04
  • @BasileStarynkevitch & underscore_d : Thank you for the details, you were absolutely right, the answer has been updated ! – Quentin Aug 01 '16 at 07:17