[comp.lang.c] Turbo C and "empty" arrays

gp@picuxa.UUCP (Greg Pasquariello X1190) (07/09/87)

	Okay, yet another problem with Turbo C (which I happen to
	think is an excellent compiler :-).  I have a 'C' code generator
	that sometimes generates empty arrays, i.e.

		char	array_name[] = {
			};

	This is perfectly normal for the code generator, for reasons
	to detailed to explain here.  The problerm is, when I try
	to compile this construct with Turbo C, I get an invlaid 
	initialization error. This does NOT happen with Microsoft C,
	or Lattice C.  Any comments, suggestions (fixes) ?  Thanks
	in advance.

	Greg
====================
"We thank you for your support"  Mr B.

guy%gorodish@Sun.COM (Guy Harris) (07/10/87)

> 	Okay, yet another problem with Turbo C (which I happen to
> 	think is an excellent compiler :-).  I have a 'C' code generator
> 	that sometimes generates empty arrays ...
> 	This is perfectly normal for the code generator, for reasons
> 	to detailed to explain here.

That's not a problem with Turbo C, that's a problem with your code
generator.   That construct is *ILLEGAL* C; check out either the K&R
or ANSI C draft grammars - they clearly indicate that an initializer
of "{}" is NOT valid, and that there must be at least one
initializer.  The fact that some compilers happen to accept it is
irrelevant; that is NOT a reason why other compilers should.  Turbo C
is perfectly within its rights to complain.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com

gwyn@brl-smoke.ARPA (Doug Gwyn ) (07/12/87)

In article <23127@sun.uucp> guy%gorodish@Sun.COM (Guy Harris) writes:
>... they clearly indicate that an initializer of "{}" is NOT valid,
>and that there must be at least one initializer.

This is perhaps a good opportunity to remind people that I'm the POC for
a special interest group concerning 0-sized data objects in C (which
aren't currently legal).  If you have good examples that illustrate
just why you need 0-sized objects, send them to me and I'll use them as
ammunition in support of legalizing them.

dg@wrs.UUCP (David Goodenough) (07/14/87)

In article <6105@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
>In article <23127@sun.uucp> guy%gorodish@Sun.COM (Guy Harris) writes:
>>... they clearly indicate that an initializer of "{}" is NOT valid,
>>and that there must be at least one initializer.
>
>This is perhaps a good opportunity to remind people that I'm the POC for
>a special interest group concerning 0-sized data objects in C (which
>aren't currently legal).  If you have good examples that illustrate
>just why you need 0-sized objects, send them to me and I'll use them as
>ammunition in support of legalizing them.

One place where a zero sized array would have been useful was in a tree
structure module I once wrote:

struct node
 {
    struct node *parent;
    struct node *left;
    struct node *right;
    int object[0];		/* << zero sized declaration */
 }

Now when I want to allocate memory for a node that can hold anything
I could write:

	newnode = node_alloc(sizeof(object));

where node_alloc() is declared:

node_alloc(size)
unsigned size;
 {
    return(malloc(sizeof(struct node) + size);
 }

My reason for the zero sized object in struct node is that the
sizeof(struct node) simply gets space for the three pointers, and size is
responsible for the object - my intention was to keep the two bits quite
distinct, yet still allow for variable sized objects.
--
		dg@wrs.UUCP - David Goodenough

					+---+
					| +-+-+
					+-+-+ |
					  +---+

gp@picuxa.UUCP (Greg Pasquariello X1190) (07/15/87)

	It seems that my question about empty arrays and Turbo 'C'
	was extremely stupid.  It seems the problem was with my code
	generator, which SHOULD NOT generate empty arrays.  My apologies
	to all who felt they must help this moron, and others who
	were so offended that they didn't help.  

	Apparently, once in a while, I get so damn cocky that I forget
	a 'C' reference exists.  As was pointed out by many, empty
	structures are illegal.

	Thanks to all who responded.

	Greg.

==========================
	"We thank you for your support" Mr B.

	Confidential to Ralph:  No, I haven't seen the one with the
	scoreboard, but have you seen the one in which their product
	"considerably improves the flavor of the ice"?