Zero Length Arrays Allowed in C Standard?

P E Smee exspes at gdr.bath.ac.uk
Tue Dec 12 22:00:42 AEST 1989


In article <8129 at cg-atla.UUCP> fredex at cg-atla.UUCP (Fred Smith) writes:
>Excuse me, but I must ask a stupid question. Why the !@#$ would anyone even want
>to declare an array of zero size ???? 

One reason I haven't seen given yet is to provide a marker for the end
of an entity.  I could use such a beast (more or less) as follows
(example slightly simplified)...

    struct fred {
	struct fred * last;
	struct fred * next;
	long offset;
	int thing;
	   ...  bunch of more data stuff
	char end_marker[0];         /* MUST BE LAST IN STRUCTURE */
	};

    ....

    bzero (&fred.offset, &fred.end_marker - &fred.offset);

to provide an easy way of clearing a member of the list without
disturbing its 2 list links (the first 2 pointers).  At present I use
an int end_marker as I don't trust size 0 arrays.  This is quicker than
zeroing each item in the struct separately, and I don't have to worry
about possible padding -- where I would if I tried to work out the size
of the bzero from the sizes of the data items in the region I want to
zap.

-- 
 Paul Smee, Univ. of Bristol Comp. Centre, Bristol BS8 1TW (Tel +44 272 303132)
 Smee at bristol.ac.uk   :-)   (..!uunet!ukc!gdr.bath.ac.uk!exspes if you HAVE to)



More information about the Comp.lang.c mailing list