如果我有一个看起来像这样的原型:
If I have a prototype that looks like this:
function(float,float,float,float)
我可以传递这样的值:
function(1,2,3,4);
如果我的原型是这样的:
So if my prototype is this:
function(float*);
有什么办法可以实现这样的目标吗?
Is there any way I can achieve something like this?
function( {1,2,3,4} );
只是在寻找一种懒惰的方法来做到这一点而不创建临时变量,但我似乎无法确定语法.
Just looking for a lazy way to do this without creating a temporary variable, but I can't seem to nail the syntax.
您可以在 C99(但不是 ANSI C (C90) 或 C++ 的任何当前变体)中使用 复合文字.有关详细信息,请参阅 C99 标准的第 6.5.2.5 节.举个例子:
You can do it in C99 (but not ANSI C (C90) or any current variant of C++) with compound literals. See section 6.5.2.5 of the C99 standard for the gory details. Here's an example:
// f is a static array of at least 4 floats
void foo(float f[static 4])
{
...
}
int main(void)
{
foo((float[4]){1.0f, 2.0f, 3.0f, 4.0f}); // OK
foo((float[5]){1.0f, 2.0f, 3.0f, 4.0f, 5.0f}); // also OK, fifth element is ignored
foo((float[3]){1.0f, 2.0f, 3.0f}); // error, although the GCC doesn't complain
return 0;
}
GCC 也将此作为 C90 的扩展提供.如果您使用 -std=gnu90(默认值)、-std=c99 或 -std=gnu99 编译,它将编译;如果使用 -std=c90 编译,则不会.
GCC also provides this as an extension to C90. If you compile with -std=gnu90 (the default), -std=c99, or -std=gnu99, it will compile; if you compile with -std=c90, it will not.
这篇关于如何在不使用变量 C/C++ 的情况下将常量数组文字传递给采用指针的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何在 C++ 中读取和操作 CSV 文件数据?How can I read and manipulate CSV file data in C++?(如何在 C++ 中读取和操作 CSV 文件数据?)
在 C++ 中,为什么我不能像这样编写 for() 循环:In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,为什么我不能像这样编写 for() 循环: for(
OpenMP 如何处理嵌套循环?How does OpenMP handle nested loops?(OpenMP 如何处理嵌套循环?)
在循环 C++ 中重用线程Reusing thread in loop c++(在循环 C++ 中重用线程)
需要精确的线程睡眠.最大 1ms 误差Precise thread sleep needed. Max 1ms error(需要精确的线程睡眠.最大 1ms 误差)
是否需要“do {...} while ()"?环形?Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?环形?)