Path: utzoo!utgpu!water!watmath!clyde!rutgers!umd5!uvaarpa!mcnc!rti!xyzzy!throopw
From: throopw@xyzzy.UUCP (Wayne A. Throop)
Newsgroups: comp.lang.c
Subject: Re: Data initialization -- a major problem.
Message-ID: <452@xyzzy.UUCP>
Date: 16 Dec 87 20:42:48 GMT
References: <2422@druco.ATT.COM>
Organization: Data General, RTP NC.
Lines: 104

> connors@druco.ATT.COM (ConnorsPA)
> [...] different compilers will SILENTLY interpret the data
> in different ways:
>     struct s1 { int a; int b; int c; };
>     struct s2 { struct s1 sarr[2]; };
>     struct s2 SS[2] = { {2, 3},		/* ROW A */
>		          {4, 5}		/* ROW B */
>     };

The problem being that some compilers take the 4 to be initializing
SS[0].sarr[1].a, and others take it to be initializing SS[1].sarr[0].a.

> Whatever the answer is, we are left with the problem
> that current compilers (I have looked at eight) operate under
> different assumptions.
> But what IS the answer?

Always apply a curly-bracketed list of initializers for every agregate,
and a non-curly-bracketed expression for every primitive type.  When all
agregates have a curly-bracketed list, and all primitive types have
non-bracketed expressions, I don't know of a C compiler what won't
assign the values where they are intended.  For example, I compiled and
debugged this program:

    struct s1 { int a; int b; int c; };
    struct s2 { struct s1 sarr[2]; };
    struct s2 SS1[2] = {{{{ 2, 3 }, { 4, 5 }}}};
    struct s2 SS2[2] = {{{{ 2, 3 }}}, {{{ 4, 5 }}}};

    int main(){ return(0); }

with these results:

    (dbx) print SS1
    {
    {
     sarr = {
    {
     a =  000000000002,
     b =  000000000003,
     c =  000000000000
    },
    {
     a =  000000000004,
     b =  000000000005,
     c =  000000000000
    }
    }
    },
    {
     sarr = {
    {
     a =  000000000000,
     b =  000000000000,
     c =  000000000000
    },
    {
     a =  000000000000,
     b =  000000000000,
     c =  000000000000
    }
    }
    }
    } 
    (dbx) print SS2
    {
    {
     sarr = {
    {
     a =  000000000002,
     b =  000000000003,
     c =  000000000000
    },
    {
     a =  000000000000,
     b =  000000000000,
     c =  000000000000
    }
    }
    },
    {
     sarr = {
    {
     a =  000000000004,
     b =  000000000005,
     c =  000000000000
    },
    {
     a =  000000000000,
     b =  000000000000,
     c =  000000000000
    }
    }
    }
    } 
    (dbx) 

--
I cain't git a long little doggie,
I cain't even git one that's small...
I cain't git a long little doggie,
I cain't git a doggie ay-tall.
                                        --- Yosemite Sam
-- 
Wayne Throop      !mcnc!rti!xyzzy!throopw