bmiColors field of BITMAPINFO structure

后端 未结 3 1538
孤独总比滥情好
孤独总比滥情好 2020-12-21 19:03

The BITMAPINFO structure has the following declaration

typedef struct tagBITMAPINFO {
    BITMAPINFOHEADER bmiHeader;
    RGBQUAD bmiColors[1];
         


        
3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-21 19:40

    There's no static keyword in the declaration. It's a completely normal struct member. It's used to declare a variable-sized struct with a single variable-sized array at the end

    The size of the array is only known at compile time, but since arrays of size 0 are forbidden in C and C++ so we'll use array[1] instead. See the detailed explanation from MS' Raymond Chen in Why do some structures end with an array of size 1?

    On some compilers like GCC zero-length arrays are allowed as an extension so Linux and many other platforms usually use array[0] instead of array[1]

    Declaring zero-length arrays is allowed in GNU C as an extension. A zero-length array can be useful as the last element of a structure that is really a header for a variable-length object:

    struct line {
      int length;
      char contents[0];
    };
    
    struct line *thisline = (struct line *)
      malloc (sizeof (struct line) + this_length);
    thisline->length = this_length;
    

    Arrays of Length Zero

    In C99 a new feature called flexible array member was introduced. Since then it's better to use array[] for portability

    struct vectord {
        size_t len;
        double arr[]; // the flexible array member must be last
    };
    

    See also

    • What is the purpose of a zero length array in a struct?
    • What's the need of array with zero elements?
    • What is the advantage of using zero-length arrays in C?

提交回复
热议问题