This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

declare a variable with flexible array members (c99)

Hello everyone,

in c99 I can make a struct with a flexible array member at the end. Is it possible to create such a variable at compile time?

e.g:

struct monitoredArray
{
   unsigned int const arrSize;
   unsigned int nUsed;
   uint8_t array[];
};

static struct monitoredArray myArray = {10, 0, [10] /* this won't work... */};

I'd like to use this for a generic kind of initialization (in respect to the array size) so I can use this in different modules without big modification or use of malloc and an init-function. I just want to know how to write that [10] in the example above.

Thanks
Alexander

Parents
  • I don't want to create to initialize the array elements. The purpose is to write a managed container for data like a fifo. The user should be able to create a variable of that type and use it with provided functions:

    fifoRet_t fifo_push(fifo_t * pFifo, int elem)
    fifoRet_t fifo_pop(fifo_t * pFifo, int * elem)
    


    I want it to be as convenient as possible, so I don't want to use a init function but provide a generic size for the buffer.

    My current solution is the following:

    typedef struct fifo_s
    {
      uint32_t       nextRead;
      uint32_t       nWritten;
      uint32_t const sizePerElement;
      uint32_t const nElements;
      int *    const container;
    }fifo_t;
    
    #define FIFO_DECLARE(name,numElements)                             \ 
      int name##_container[ (numElements) ];                           \ 
      fifo_t name = {0,0,(numElements) , (& name##_container[0])}
    


    What I don't like about this is that it is not possible to create both the container and the fifo_t element static with

    static FIFO_DECLARE(foo,128);
    


    Then I was reading about the new language features in c99 and read about the flexible array elements. Now I think I can create my buffer with those.

Reply
  • I don't want to create to initialize the array elements. The purpose is to write a managed container for data like a fifo. The user should be able to create a variable of that type and use it with provided functions:

    fifoRet_t fifo_push(fifo_t * pFifo, int elem)
    fifoRet_t fifo_pop(fifo_t * pFifo, int * elem)
    


    I want it to be as convenient as possible, so I don't want to use a init function but provide a generic size for the buffer.

    My current solution is the following:

    typedef struct fifo_s
    {
      uint32_t       nextRead;
      uint32_t       nWritten;
      uint32_t const sizePerElement;
      uint32_t const nElements;
      int *    const container;
    }fifo_t;
    
    #define FIFO_DECLARE(name,numElements)                             \ 
      int name##_container[ (numElements) ];                           \ 
      fifo_t name = {0,0,(numElements) , (& name##_container[0])}
    


    What I don't like about this is that it is not possible to create both the container and the fifo_t element static with

    static FIFO_DECLARE(foo,128);
    


    Then I was reading about the new language features in c99 and read about the flexible array elements. Now I think I can create my buffer with those.

Children
No data