Extract Argument from C Macro -
i have number of definitions consisting of 2 comma-separated expressions, this:
#define pin_alarm gpioc,14
i want pass second expression of definitions (14 in case above) unary macros following:
#define _pin_mode_output(n) (1u << ((n) * 2u))
how can extract second number? want macro, call "pick_right", me:
#define pick_right(???) ???
so can make new macro can take "pin" definitions:
#define pin_mode_output(???) _pin_mode_output(pick_right(???))
and can do:
#define result pin_mode_output(pin_alarm)
do not use macros this. if must, following work throwing away left part first number remains. use care. no guarantees.
#define pin_alarm gpioc,14 #define rightpart_only(a,b) b #define pin_mode_output(a) rightpart_only(a) #define result pin_mode_output(pin_alarm) int main (void) { printf ("we'll pick ... %d\n", pin_mode_output(pin_alarm)); printf ("or maybe %d\n", result); return 0; }
if want left part string, can use (with same warnings above), left part gets converted string #
:
#define leftpart_only(a,b) #a #define pin_mode_name(a) leftpart_only(a)
there practical reason not entirely without problems. gpioc
symbol , such possibly defined elsewhere. fortunately, not problem if undefined, or simple type - after all, first thing macros "throw away left part". jonathan leffler comments
note if
gpioc
maps macro containing commas, you're compilation errors.
Comments
Post a Comment