[comp.lang.c] Static list initialization - thanks

ajw@donk.UUCP (ajw) (10/19/88)

I posted the following a few days ago (slightly edited):

> 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.
> Question: how to achieve this initialization legally, leaving 'head'
> and 'tail' static?

Thanks to those who replied, and particularly to schaefer@cse.ogc.edu
(Bart Schaefer) who first suggested what I should have been able to
work out for myself:

    static struct whatever h_and_t[] = {
      { &h_and_t[1], (struct whatever *)0 },
      { (struct whatever *)0, &h_and_t[0] }
    };
    #define head (h_and_t[0])
    #define tail (h_and_t[1])

-- Alan Waldock                 ...{tektronix|sun}!ogcvax!omepd!ihf1!mdt!ajw
Intel Corp, HF2-37               OR            ...uunet!littlei!ihf1!mdt!ajw
5200 NE Elam Young Pkwy           OR EVEN               ajw@mdt.hf.intel.com
Hillsboro, OR. 97124-6497          OR IF ALL ELSE FAILS       (503) 696-2478
                                
Opinions individual author's. Read before breaking seal. No warranty implied.