forward declaration of array; how?

Karl Heuer karl at haddock.ima.isc.com
Thu Oct 12 03:26:44 AEST 1989


In article <2364 at munnari.oz.au> ok at cs.mu.oz.au (Richard O'Keefe) writes:
>	extern void (*(dispatch[]))();
>	static void (*(dispatch[]))() = { ... foo, ..., baz, ... };

The ANSI way to do this is to use `static' rather than `extern' in the first
declaration.  This makes it a `tentative definition', which can be thought of
as a kludge around the ambiguity between declarations and uninitialized
definitions; it retroactively becomes a definition only if no real definition
appears before the end of the translation unit.  In this case, the initialized
declaration later on would be the real definition, causing the earlier one to
be treated as a forward declaration only.

Unfortunately, not all existing pre-ANSI compilers agree with this.  Even more
unfortunately, some do not have *any* way to forward-declare a static object,
so you can't just use `#if __STDC__ static #else extern #endif' and expect it
to be portable.  For full portability (without rearranging it to be a backward
reference), you'd need to make the `dispatch' object global instead of static.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint



More information about the Comp.lang.c mailing list