Have macro 'return' a value

后端 未结 9 1843
闹比i
闹比i 2021-01-31 10:12

I\'m using a macro and I think it works fine -

#define CStrNullLastNL(str) {char* nl=strrchr(str,\'\\n\'); if(nl){*nl=0;}}

So it works to zero out the last

9条回答
  •  醉话见心
    2021-01-31 10:48

    I gave +1 to Mike because he's 100% right, but if you want to implement this as a macro,

    char *CStrNullLastNL_nl; // "private" global variable
    #define nl ::CStrNullLastNL_nl // "locally" redeclare it
    #define CStrNullLastNL( str ) ( \
        ( nl = strrchr( str, '\n') ), /* find newline if any */ \
        nl && ( *nl = 0 ), /* if found, null out */ \
        (char*) nl /* cast to rvalue and "return" */ \
    OR  nl? str : NULL /* return input or NULL or whatever you like */
    )
    #undef nl // done with local usage
    

提交回复
热议问题