Well, sort of, but this is probably not what you want:
struct token { // your fields size_t item_size; size_t length }; struct token *make_token(/* your arguments */, size_t item_size, size_t length) { struct token *t = malloc(sizeof *t + item_size * length); if(t == NULL) return NULL; t->item_size = item_size; t->length = length; // rest of initialization }
The following macro can be used to index your data (assuming x is a struct token * ):
#define idx(x, i, t) *(t *)(i < x->length ? sizeof(t) == x->item_size ? (void *)(((char *)x[1]) + x->item_size * i) : NULL : NULL)
And, if you want, the following macro can wrap your make_token function to make it more intuitive (or more hacky if you think so):
#define make_token(, t, l) (make_token)(, sizeof(t), l)
Using:
struct token *p = make_token(, int, 5);
source share