static list initialization

joe at modcomp.UUCP joe at modcomp.UUCP
Mon Oct 17 01:13:00 AEST 1988


> Here's a fragment of code I find myself staring at glumly from time to time.
> It sets up the nucleus of a 2-way list, into which extra malloc'd nodes will
> be inserted.
>
>     struct whatever {
>         struct whatever *next;            /* next node */
>         struct whatever *prev;            /* previous node */
>         int    eresting_stuff;            /* blah, blah, ... */
>     };
>     extern struct whatever tail;
>     static struct whatever head = { &tail, (struct whatever *)0 };
>     static struct whatever tail = { (struct whatever *)0, &head };
>
> Without the 'extern', the compiler can't initialize 'head'.  But with it, I
> get a 'non-standard extension' warning, although everything works just fine.

This kind of initialization can only be safely done at run time.  Even if
you found a compiler that would accept such an unethical sequence of code,
(two variables named "tail" -- wow!), you should not use it -- it would be
unportable, even to a different C compiler on the same machine/OS.  However,
I see how being able to forward reference structures, just as easily as we
can forward reference structure pointers today, could be appealing.

Joe Korty
uunet!modcomp!joe



More information about the Comp.lang.c mailing list