Assign multiple values to array in C

前端 未结 8 1280
无人共我
无人共我 2020-12-01 14:01

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         


        
8条回答
  •  我在风中等你
    2020-12-01 14:27

    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

    static inline void set_coordinates(
            GLfloat coordinates[static 8],
            GLfloat c0, GLfloat c1, GLfloat c2, GLfloat c3,
            GLfloat c4, GLfloat c5, GLfloat c6, GLfloat c7)
    {
        coordinates[0] = c0;
        coordinates[1] = c1;
        coordinates[2] = c2;
        coordinates[3] = c3;
        coordinates[4] = c4;
        coordinates[5] = c5;
        coordinates[6] = c6;
        coordinates[7] = c7;
    }
    

    and then simply call

    GLfloat coordinates[8];
    // ...
    set_coordinates(coordinates, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f);
    

提交回复
热议问题