[comp.std.c] Tentative defs, type compatibility.

am@cl.cam.ac.uk (Alan Mycroft) (05/25/89)

Consider the following dpANS C program:
   int x[10];
   int x[] = { 1,2 };
Does the x[10] and x[] get unified together before the initialisation of
x[10] to {1,2,0,0,...} or does the initialisation of x to (int[2]){1,2}
happen first and cause a type conflict with x[10]?

I can see arguments both way (more programs are legal vs. lack of
symmetry -- most other top-level declarations are order-independent), but
the dpANS does not immediately explain.  Views?

gwyn@smoke.BRL.MIL (Doug Gwyn) (05/27/89)

In article <754@scaup.cl.cam.ac.uk> am@cl.cam.ac.uk (Alan Mycroft) writes:
>Consider the following dpANS C program:
>   int x[10];
>   int x[] = { 1,2 };

>Does the x[10] and x[] get unified together before the initialisation of
>x[10] to {1,2,0,0,...} or does the initialisation of x to (int[2]){1,2}
>happen first and cause a type conflict with x[10]?

The technical question about the wording in the Standard comes down to,
is "x" in the second declaration an "array of unknown size"?  I think
we would agree that IN THAT DECLARATION it has an INCOMPLETE TYPE, but
I think its SIZE is known anyway since by the wording about tentative
definitions both declarations refer to the same object, and the size is
known after the first declaration.

Personally I'm not too happy about the identifier having incomplete
type even though its size if already known, but it doesn't seem to
cause any technical problems.

DISCLAIMER:
This is, of course, not an official X3J11 ruling on the question.
I suggest you submit this to X3J11 as a request for interpretation.

ka@june.cs.washington.edu (Kenneth Almquist) (05/30/89)

am@cl.cam.ac.uk (Alan Mycroft) writes:
> Consider the following dpANS C program:
>    int x[10];
>    int x[] = { 1,2 };
> Does the x[10] and x[] get unified together before the initialisation of
> x[10] to {1,2,0,0,...} or does the initialisation of x to (int[2]){1,2}
> happen first and cause a type conflict with x[10]?

In traditional C, the latter.  From K&R Edition 1, page 195:

	The first constant-expression [i.e. the expression in the
	leftmost "[]" in an array declaration] may also be omitted
	when the declarator is followed by initialization.  In this
	case the size is calculated from the number of initial
	elements supplied.

This is useful when an array is declared in a header file and initialized
in a C file.  By omitting the array size from the initialization, you can
have the compiler check that you gave the right number of initializers.

The lack of clarity in the dpANS suggests that X3J11 did not intend to
differ from traditional C.  As Doug Gwyn notes in <10330@smoke.BRL.MIL>,
the only way to be sure is to submit a query to X3J11.
					Kenneth Almquist