Hi all,

Having a little trouble with assigning values to an array within a C struct. Unfortunately i cannot assign a size to the array as i am reading values to an array to be stored, and the size is relative to the other values in the struct passed to createGeo(); the function.

The issue here is the float *coords - which i want to be filled with values in the createGeo() function.

    typedef struct geom{
    float dimen[3];//width, height, length
    int totalQuads;
    float *coords;
    GLubyte indices[];

    geo Base;

    void createGeo(geo inst);

    int main(int argc, char *argv[])

      Base.dimen[0] = 11;
      Base.dimen[1] = 0.15;
      Base.dimen[2] = 44;

      return 0;

    void createGeo(geo inst)
    int i,j;
    int jump = 0;
    //find total of vertices required  - div by 1
    //int x_div = inst.dimen[0]/inst.dimen[0];
    //int z_div = inst.dimen[2]/inst.dimen[2];

    inst.totalQuads = (inst.dimen[0] * inst.dimen[2]) * 4;

    //indices array
    for(i = 0; i < inst.totalQuads; i++)
    inst.indices[i] = i;

    //assign memory
    inst.coords = malloc(inst.totalQuads * 3 * sizeof(float));

    //perhaps i should turn this into local array

    //now make geom -
    //length form - to 0
    for(i = -inst.dimen[2]; i < 0; i++)
    for(j = 0; j < inst.dimen[0]; j++)
    inst.coords[0+jump][j][0] = j;
    inst.coords[1+jump][j][1] = 0;
    inst.coords[2+jump][j][2] = i;

    inst.coords[3+jump][j][0] = j;
    inst.coords[4+jump][j][1] = 0;
    inst.coords[5+jump][j][2] = i;

    inst.coords[6+jump][j][0] = j;
    inst.coords[7+jump][j][1] = 0;
    inst.coords[8+jump][j][2] = i;

    inst.coords[9+jump][j][0] = j;
    inst.coords[10+jump][j][1] = 0;
    inst.coords[11+jump][j][2] = i;




The solution is probably something really straightforward but i can't seem to get anywhere. Perhaps i should add the values to a internal float array and then do 

inst.coords = my_array;

Grateful for your help!

5 Years
Discussion Span
Last Post by n.cramp

GLubyte indices[];

Declare that as a pointer and allocate memory for it on line 35, after you know how big it should be.

void createGeo(geo inst)

Pass that object by reference, not by value. such as void createGeo(geo& inst)so that any changes made by createGeo() are reflected in the object created in main()


Thanks i'll try that. I always get a bit confused with passing pointers around, best do some reading on that.


That's right -- this is supposed to be C, not C++. C uses pointers, not references. so you want createGeo(geo* inst), then replace . with ->, such as inst->coords[0+jump][j][0] = j;

Edited by Ancient Dragon: edit


Thanks for your help, i realised i had to alter the code a little but incorporated your suggestions and they helped a lot. I enjoy using C, C++ seems a little too complex for my needs! But C is perfect.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.