Indefinite-length array as member of struct: how?

Elwood C. Downey ecd at ncs-med.UUCP
Thu Jul 20 04:31:57 AEST 1989


You might make an array member of length 1 at the end of a struct, then malloc
enough room for all that you really need and index into it via the array.
for example:

struct s {
    ...			/* anything ... */
    char array[1];	/* must be last member */
};

f(n)
int n;
{
	struct s *sp;

	sp = malloc (sizeof(struct s) + n - 1); /* you get 1 already */
	/* now you can use sp->array[0..n-1] */
}



More information about the Comp.lang.c mailing list