Zero Length Arrays Allowed in C Standard?

D'Arcy J.M. Cain darcy at druid.uucp
Fri Dec 15 02:22:12 AEST 1989


In article <1989Dec12.110042.29290 at gdt.bath.ac.uk> exspes at gdr.bath.ac.uk (P E Smee) writes:
>
>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);
>
This looks like a good idea but I don't understand why it must be the
last element.  How about the following:

    struct fred {
	struct fred * last;
	long offset;
	int thing;
	   ...  bunch of more data stuff
	char end_marker[0];
	struct fred * next;
	};

Then your bzero still works but on a chunk of the structure internal to
the structure.  You could also use the address of next but the above 
should still work (assuming of course an ANSI standard different from what
we have.)

Of course I am assuming that this mythical construct would act as it does in
assembler where;

LABEL1:
LABEL2:
	dw	0

evaluates both LABEL1 and LABEL2 as pointing to the same memory location.

This is fun.  Maybe we need a comp.lang.mythical :-)


-- 
D'Arcy J.M. Cain (darcy at druid)     |   Thank goodness we don't get all 
D'Arcy Cain Consulting             |   the government we pay for.
West Hill, Ontario, Canada         |
No disclaimers.  I agree with me   |



More information about the Comp.lang.c mailing list