How's flexible array implemented in c? -


.. char arkey[1]; } bucket; 

the above said flexible array,how?

often last member of struct given size of 0 or 1 (despite 0 being against standard pre-c99, it's allowed in compilers has great value marker). 1 not create array of size 0 or 1, indicates fellow coders field used start of variably sized array, extending final member available memory.

you may find member of struct defining exact length of flexible array, find member contains total size in bytes of struct.

links

example

typedef struct {     size_t len;     char arr[]; } mystring;  size_t mystring_len(mystring const *ms) { return ms->len; }  mystring *mystring_new(char const *init) {     size_t len = strlen(init);     mystring *rv = malloc(sizeof(mystring) + len + 1);     rv->len = len;     strncpy(rv->arr, init, len);     return rv; } 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -