carraysinitializationdeclaration

Assign multiple values to array in C


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, ...};?


Solution

  • 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;