Assign multiple values to array in C Assign multiple values to array in C c c

Assign multiple values to array in C


If you really to assign values (as opposed to initialize), you can do it like this:

 GLfloat coordinates[8];  static const GLfloat coordinates_defaults[8] = {1.0f, 0.0f, 1.0f ....}; ...  memcpy(coordinates, coordinates_defaults, sizeof(coordinates_defaults)); return coordinates; 


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:

struct foo {  GLfloat arr[10];};...struct foo foo;foo = (struct foo) { .arr = {1.0, ... } };


The old-school way:

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;