karl@haddock.ima.isc.com (Karl Heuer) (02/24/89)
In comp.lang.c article <437@lakart.UUCP> dg@lakart.UUCP (David Goodenough) suggests that union initialization could be done thus: >union { float f; int i; char *c; } u[3] = { > { 1.0 ; ; }, /* could also be { 1.0 } - trailing ; are optional */ > { ; 76 ; }, /* ditto: { ; 76 } would also be OK */ > { ; ; "STUG" } >}; Not bad, but I see no reason to use semicolon as the separator. Comma is already used in this context for struct initializers, so it would be more consistent to use the same token for unions. (No, this would not conflict with the comma operator.) In fact, one could even generalize the notation to allow missing expressions in a struct or array initializer: int a[3]={3, ,5} would initialize a[0]=3, a[2]=5, and leave a[1] uninitialized (zero or garbage, depending on storage duration). However, initializing by position may be a mistake anyway; it requires the application to know the order of the members. This information is often deliberately left unspecified. If we're going to tweak the language, let's try to do it in a way that will assist with this sort of data abstraction. Would the following be workable? int a[3] = { 0: 3, 2: 5 }; union { float f; int i; char *c; } u[3] = { { f: 1.0 }, { i: 76 }, { c: "STUG" } }; Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint Followups to comp.lang.misc; we're talking `D' again.
c08_d103@jhunix.HCF.JHU.EDU (Ex-God) (02/25/89)
In article <11860@haddock> karl@haddock.ima.isc.com (Karl Heuer) asks: >Would the following be workable? > int a[3] = { 0: 3, 2: 5 }; Yes, but as arrays are inherently ordered, there's no reason to ignore the ordering -- the comma-separated format is much better for small arrays: int a[3] = {0, , 2}; > union { float f; int i; char *c; } u[3] = { > { f: 1.0 }, > { i: 76 }, > { c: "STUG" } > }; In the case of unions and structures, on the other hand, this idea makes sense, as they are ordered only because it is necessary. The only question is whether it is more important to make all initialization consistent, or to make all initializations look clear and obvious. It would probably be best to allow any complex type to use either the labelled format or the comma-separated format -- it might be useful to intialize the first and 230th elements of an array with the labelled format, but it would be easier to initialize the first 100 elements except the 3rd with the comma-separated format. Andrew Barnert (Smilin' Jack // ex-God // etc.) ins_balb@jhunix