Variable-length string at end of structure

Sam Kendall kendall at wjh12.UUCP
Tue Jun 19 08:17:11 AEST 1984

I am wondering how many programs use the following construct, or
something similar:

	struct a {
		char varlen_string[1];
	} a_struct;
	p = (struct a *) malloc(sizeof (struct a) + strlen(a_string));
	... /* fill in structure members */
	(void) strcpy(p->varlen_string, a_string);

That is, malloc'ing space for a fixed-length structure plus a
variable-length string, and referencing the string using the last member
of the structure.

The Rand Editor and its derivatives do this, and Martin Minow's cpp does
it; has anyone seen other programs that do?  I'd be interested to know
how many programs do this, and exactly what type the last member of the
structure is (i.e.  is it char [1]?  char?  Something else?) I need to
know in order to put some kludge in our runtime checker to handle it;
currently it is flagged as an error.

	Sam Kendall	{allegra,ihnp4,ima,amd70}!wjh12!kendall
	Delft Consulting Corp.	    decvax!genrad!wjh12!kendall

More information about the Comp.lang.c mailing list