如何在 C 中 #define 数组?
以下定义数组的语法在 C 中是否有效?
#define array[] { \
for (j=0; j<N_frequencysteps;j++) \
{ \
array[j] = (function); \
} \
}
如果没有,如何在 C 中定义数组?
Is the following syntax to define an array valid syntax in C?
#define array[] { \
for (j=0; j<N_frequencysteps;j++) \
{ \
array[j] = (function); \
} \
}
If not, how do I define an array in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于您如何定义“有效语法”。
C 预处理器将接受它作为类对象宏。
C 编译器不会接受作为合法 C 的输出。
考虑一个调用:
C 编译器看到:
这是乱码(即使使用“输入代码”)这里'已删除)。
如何在 C 中定义数组?
您还可以使用初始化程序,但除非您彻底,否则可能会收到编译器警告:
另一种可能性:如果您想定义一个数组并通过调用特定函数来初始化它,那么我想您可以尝试:
这可以用作:(
请注意,此初始化程序取决于宏的实现细节 - 这不是一个好主意。但无论如何,我认为我不会使用这样的宏。)这取决于您是否处于 C99 模式,如果在特定的代码块中您有多个这样的代码(因为它会将代码与声明混合在一起) - 而且还因为
for
循环中的声明仅在 C++ 和 C99 中受支持,在C90。It depends on how you define 'valid syntax'.
The C pre-processor will accept it as an object-like macro.
The C compiler will not accept the output as legitimate C.
Consider an invocation:
The C compiler sees:
That is gibberish (even with the 'enter code here' removed).
How can you define an array in C?
You could also use an initializer, but you might get compiler warnings unless you are thorough:
One more possibility: if you want to define an array and initialize it with calls to a specific function, then you could, I suppose, try:
This could be used as:
(Note that this initializer depends on an implementation detail of the macro - not a good idea. But I don't think I'd ever use such a macro, anyway.) It depends on you being in C99 mode if you have more than one of these in a particular block of code (since it would then mix code with declarations) - and also because the declaration in the
for
loop is only supported in C++ and C99, not in C90.这看起来确实是错误的。为什么不以正常的方式定义数组呢?
This looks really wrong. Why not define array in a normal way?