C语言 C 错误:宏名称后缺少空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7321593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C error: missing whitespace after the macro name
提问by Hannesh
I wrote the following macro:
我写了以下宏:
#define m[a,b] m.values[m.rows*(a)+(b)]
However gcc gives me this error:
但是 gcc 给了我这个错误:
error: missing whitespace after the macro name
What is wrong and how do I fix it?
出了什么问题,我该如何解决?
回答by cdhowie
You cannot use [and ]as delimiters for macro arguments; you must use (and ). Try this:
不能使用[and]作为宏参数的分隔符;你必须使用(和)。尝试这个:
#define m(a,b) m.values[m.rows*(a)+(b)]
But note that defining the name of a macro as the name of an existing variable may be confusing. You should avoid shadowing names like this.
但请注意,将宏的名称定义为现有变量的名称可能会造成混淆。您应该避免像这样隐藏名称。
回答by Doug T.
I'm not familiar with any C preprocessor syntax that uses square brackets. Change
我不熟悉任何使用方括号的 C 预处理器语法。改变
#define m[a,b] m.values[m.rows*(a)+(b)]
to
到
#define m(a,b) m.values[m.rows*(a)+(b)]
And it should work.
它应该工作。
回答by Armen Tsirunyan
You cannot have such a macro that will expand when you supply arguments in squarebrackets. Wherever you got the idea that macros are a smarttext-substituting tool, it's just the other way round: macros are extremely obtuse and stupidtext-substitution mechanism. What you're trying to do with a macro is absolutely unwarranted - just write a named function.
当您在方括号中提供参数时,您不能拥有这样一个会扩展的宏。无论您从哪里了解到宏是一种智能的文本替换工具,但事实恰恰相反:宏是极其迟钝和愚蠢的文本替换机制。你试图用宏做什么是绝对没有根据的 - 只需编写一个命名函数。

