e01.org

Flexible array member

C struct data types may end with a flexible array member with no specified size: typedef struct { size_t len; // there must be at least one other data member double arr[]; // the flexible array member must be last // The compiler may reserve extra padding space here, like it can between struct members DoubleArray; Typically, such structures serve as the header in a larger, variable memory allocation: // see section on size and padding DoubleArray* darray = malloc(sizeof(DoubleArray) + ... * sizeof(double)); darray->len = ...; for (int i = 0; i len; i++) { darray->arr[i] = ...; // transparently uses the right type (double). Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

C struct data types may end with a flexible array member with no specified size: typedef struct { size_t len; // there must be at least one other data member double arr[]; // the flexible array member must be last // The compiler may reserve extra padding space here, like it can between struct members } DoubleArray; Typically, such structures serve as the header in a larger, variable memory allocation: // see section on size and padding DoubleArray* darray = malloc(sizeof(DoubleArray) + ... * sizeof(double)); darray->len = ...; for (int i = 0; i len; i++) { darray->arr[i] = ...; // transparently uses the right type (double) }

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Flexible array member". 1783302399. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1362779318

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·