在C中将多个值分配给数组
有没有办法以压缩的形式做到这一点?
GLfloat coordinates[8];
...
coordinates[0] = 1.0f;
coordinates[1] = 0.0f;
coordinates[2] = 1.0f;
coordinates[3] = 1.0f;
coordinates[4] = 0.0f;
coordinates[5] = 1.0f;
coordinates[6] = 0.0f;
coordinates[7] = 0.0f;
return coordinates;
像坐标= {1.0f,...};
之类的东西?
Is there any way to do this in a condensed form?
GLfloat coordinates[8];
...
coordinates[0] = 1.0f;
coordinates[1] = 0.0f;
coordinates[2] = 1.0f;
coordinates[3] = 1.0f;
coordinates[4] = 0.0f;
coordinates[5] = 1.0f;
coordinates[6] = 0.0f;
coordinates[7] = 0.0f;
return coordinates;
Something like coordinates = {1.0f, ...};
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您确实要分配值(而不是初始化),您可以这样做:
If you really to assign values (as opposed to initialize), you can do it like this:
尽管在您的情况下,只需简单的初始化即可,但有一个技巧可以将数组包装到结构中(可以在声明后初始化)。
例如:
Although in your case, just plain initialization will do, there's a trick to wrap the array into a struct (which can be initialized after declaration).
For example:
老派的方式:
The old-school way:
确切地说,你几乎明白了:
Exactly, you nearly got it:
您可以使用:
但这是编译时初始化 - 您不能在当前标准中使用该方法来重新初始化(尽管我认为在即将推出的标准中有一些方法可以做到这一点,这可能不会立即帮助您) 。
我想到的另外两种方法是,如果内容已修复,则将其隐藏:
或者提供一个看起来像初始化代码的函数:
请记住这些省略号 (
...
) 是占位符,而不是从字面上插入到代码中的东西。You can use:
but this is a compile-time initialisation - you can't use that method in the current standard to re-initialise (although I think there are ways to do it in the upcoming standard, which may not immediately help you).
The other two ways that spring to mind are to blat the contents if they're fixed:
or provide a function that looks like your initialisation code anyway:
keeping in mind those ellipses (
...
) are placeholders, not things to literally insert in the code.我采用了一种数组初始化方法:
称为
C99 数组初始化,如下所示:
在
configure.ac
中:让我的开发盒上的编译器非常满意。服务器上的编译器抱怨:
并且
对于每个元素
它根本不抱怨,例如:
我喜欢对大小的检查,并且可变参数支持比对数组初始值设定项的支持表现得更稳健。
在 https://github.com/wbreeze/davenport/pull/ 中查找带有示例代码的 PR 15/files
关于来自@paxdiablo的https://stackoverflow.com/a/3535455/608359,我喜欢它;但是,对于初始化指针前进的次数与分配给数组的元素数同步感到不安全。最坏的情况是,初始化指针超出了分配的长度。因此,PR 中的 diff 包含:
int_array_init
方法将安全地分配垃圾,如果参数少于node_ct。垃圾任务应该更容易
捕获和调试。
I went with an array initialization method:
called like,
The C99 array initialization, like this:
And in the
configure.ac
:had the compiler on my dev box perfectly happy. The compiler on the server complained with:
and
for each element
It doesn't complain at all about, for example:
I like the check on size, and that the varargs support is acting more robustly than the support for the array initializer.
Find PR with sample code at https://github.com/wbreeze/davenport/pull/15/files
Regarding https://stackoverflow.com/a/3535455/608359 from @paxdiablo, I liked it; but, felt insecure about having the number of times the initializaion pointer advances synchronized with the number of elements allocated to the array. Worst case, the initializing pointer moves beyond the allocated length. As such, the diff in the PR contains,
The
int_array_init
method will safely assign junk if the number ofarguments is fewer than the node_ct. The junk assignment ought to be easier
to catch and debug.
如果您在程序中经常执行这些相同的任务并且想要快捷方式,最简单的解决方案可能是仅添加一个函数
,然后简单地调用
If you are doing these same assignments a lot in your program and want a shortcut, the most straightforward solution might be to just add a function
and then simply call
您可以使用
_Generic
和__VA_ARGS__
编写通用数组赋值宏。它不需要传递的参数数量。但它以NULL
终止;这意味着您不能分配NULL
又名 0。(您可以输入除NULL
之外的其他内容,您知道您不会分配这些内容)。您可以为每种类型调用
GEN_ARRAY_ASSIGN_FUNC
,并将该类型添加到_Generic
参数中。并像这样使用它:
或者如果您更喜欢提供计数而不是
NULL
终止:并且:
You can use
_Generic
and__VA_ARGS__
to write a generic array assignment macro. It doesn't need the number of arguments passed. But it'sNULL
terminated; Meaning you can't assignNULL
aka 0. (You can put something else other thanNULL
which you know you won't assign).You call
GEN_ARRAY_ASSIGN_FUNC
for every type and you add the type to the_Generic
arguments.And use it like this:
Or if you prefer providing count rather than
NULL
termination:And: