Assign multiple values to array in C

前端 未结 8 1221
无人共我
无人共我 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:34

    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; 
    
    0 讨论(0)
  • 2020-12-01 14:35

    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;
    
    0 讨论(0)
提交回复
热议问题