Path: utzoo!mnetor!uunet!oddjob!hao!gatech!bloom-beacon!athena.mit.edu!wesommer From: wesommer@athena.mit.edu (William Sommerfeld) Newsgroups: comp.lang.c Subject: Re: ANSI C idea: structure literals Message-ID: <3325@bloom-beacon.MIT.EDU> Date: 28 Feb 88 20:43:16 GMT References: <56@vsi.UUCP> Sender: daemon@bloom-beacon.MIT.EDU Reply-To: wesommer@athena.mit.edu (William Sommerfeld) Distribution: comp Organization: Massachusetts Institute of Technology Lines: 45 Keywords: ANSI, structure literals In article <56@vsi.UUCP> friedl@vsi.UUCP (Stephen J. Friedl) writes: >Netlanders, > > I've had an idea for C for a long time ... >... This new item might be called "structure literals" (thanks >to Doug for the name). Richard Stallman's GCC implements an extension of this as a language extension. This is how he documents it (this is from the file "internals.texinfo" from the GNU CC distribution; it is covered by the GNU copyleft); I'm not sure I like the syntax, but it's better than nothing. * Constructor expressions are allowed. A constructor looks like a cast containing an initializer. Its value is an object of the type specified in the cast, containing the elements specified in the initializer. The type must be a structure, union or array type. As explained above, GNU C does not require the elements of the initializer to be constant. Assume that `struct foo' and `structure' are declared as shown: struct foo {int a; char b[2];} structure; Here is an example of constructing a `struct foo' with a constructor: structure = ((struct foo) {x + y, 'a', 0}); This is equivalent to writing the following: { struct foo temp = {x + y, 'a', 0}; structure = temp; } You can also construct an array. A constructed array is not an lvalue and therefore cannot be coerced into a pointer to its first element. As a consequence, the only valid way to use a constructed array is to subscript it. Here is an example of constructing an array of three elements and then choosing one of them: output = ((int[]) { 2, x, 28 }) [input];