Uns*_*tal 28 c arrays initialization declaration
有没有办法以浓缩形式这样做?
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;
Run Code Online (Sandbox Code Playgroud)
有点像coordinates = {1.0f, ...};
?
Jam*_*ran 26
如果你真的要分配值(而不是初始化),你可以这样做:
GLfloat coordinates[8];
static const GLfloat coordinates_defaults[8] = {1.0f, 0.0f, 1.0f ....};
...
memcpy(coordinates, coordinates_defaults, sizeof(coordinates_defaults));
return coordinates;
Run Code Online (Sandbox Code Playgroud)
dom*_*men 11
有一个技巧可以将数组包装成一个结构(可以在声明后初始化).
即.
struct foo {
GLfloat arr[10];
};
...
struct foo foo;
foo = (struct foo) { .arr = {1.0, ... } };
Run Code Online (Sandbox Code Playgroud)
Pav*_*aev 11
老派方式:
GLfloat coordinates[8];
...
GLfloat *p = coordinates;
*p++ = 1.0f; *p++ = 0.0f; *p++ = 1.0f; *p++ = 1.0f;
*p++ = 0.0f; *p++ = 1.0f; *p++ = 0.0f; *p++ = 0.0f;
return coordinates;
Run Code Online (Sandbox Code Playgroud)