有宏返回一个值 [英] Have macro 'return' a value

查看:93
本文介绍了有宏返回一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的宏,我认为它可以正常工作 -

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 newline in a string, really its used to chop off the linebreak when it gets left on by fgets.

因此,我想知道我是否可以从宏中返回一个值,所以它可以是叫做

So, I'm wondering if I can "return" a value from the macro, so it can be called like


func( CStrNullLastNL( cstr ) ) ;

或者我必须写一个函数

推荐答案

对于要返回值的宏,宏本身必须是一个表达式。

For a macro to "return a value", the macro itself has to be an expression. Your macro is a statement block, which cannot evaluate to an expression.

你真的应该写一个 inline 函数。它将会一样快速和更可维护。

You really ought to write an inline function. It will be just as fast and far more maintainable.

这篇关于有宏返回一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆