Not sure how to declare a particular struct.

Discussion in 'Mac Programming' started by MorphingDragon, Dec 22, 2010.

  1. MorphingDragon, Dec 22, 2010
    Last edited: Dec 22, 2010

    MorphingDragon macrumors 603

    MorphingDragon

    Joined:
    Mar 27, 2009
    Location:
    The World Inbetween
    #1
    I'm making some C code to work with SVG Vector Sprites.

    I have done all the planning on paper on how to read the SVG File, represent the paths, path styles, OpenGL\Quartz2D\GDI drawing etc and how to store them into an array.

    I want to have a single struct to hold the points for the paths, styles and matrix transformation.

    Basically I have 3 arrays. A void-ptr array for the paths and path symbols. A void-ptr array to hold the styles for the paths. An int array to hold the matrix transformation for the paths.

    What I don't know is, when declaring the struct, do I have a single pointer for the array, or do I have a pointer to a pointer. (The array's size wont be known when the struct is declared, made)
     
  2. holmesf macrumors 6502a

    Joined:
    Sep 30, 2001
    #2
    I'm not sure I fully understand the situation, but if the question is just "how do I declare a member variable to a struct which is a variable length array?" then the answer is as a single pointer (not a pointer to a pointer). When you do know the length of the array, simply allocate a portion of memory of that length, and point the pointer to it (mystruct.array = (void *)malloc( num_bytes );)
     
  3. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #3
    If you have several fixed-size elements and one variable size array, you could declare that array as the last element in the struct, eg,

    Code:
    typedef struct 
    {
    int somePts[32];
    char *lblStrings[32];
    void *sprites[0];
    } dStruct;
    in which case, the sprites array would fall completely outside the bounds of the struct: you would have to formulate an allocation something like
    Code:
    malloc( sizeof( dStruct ) + ( sizeof( void* ) * spriteCount ) );
    I believe you will find it handier to simply create an array of structs. Create a struct that can hold as much atomic data as practical (never too little) and array them. You might waste some space, but it might be easier to work with.
     

Share This Page