question about linkage

Matthew Self mself at next.com
Sun Oct 22 04:31:48 AEST 1989


Is K&R2 correct in saying (p. 227):

    If the first external declaration for a function
    or object includes the static specifier, the
    identifier has internal linkage; otherwise it has
    external linkage.

If so, then the identifier foo has external linkage in the
following program:

extern int foo[];

/* lots of references to foo here. */

static int foo[] = {0, 1, 2, 3};

How, then, can you forward declare an object with internal
linkage whose size isn't known when you forward declare it?
The logical thing to use appears to be:

static int foo[];

/* lots of references to foo here. */

static int foo[] = {0, 1, 2, 3};

Since the first declaration will be taken to be a tentative
definition of foo, which will later be demoted to a declaration
when the second definition is seen.

Unfortunately, every compiler I have tried complains that foo has
unknown size when you try this.

Also worth mentioning is the fact that traditional compilers seem
to give foo internal linkage rather than external in the first
example, indicating that this was how they solved the problem before
ANSI came along.  ANSI seems to have perverted the traditional
method for doing this without providing an alternative.

It is not possible to simply reorder the code to avoid the forward
reference, since this code is generated by a translator, and this
would require huge amounts of buffering!

Matthew Self
mself at next.com



More information about the Comp.std.c mailing list