ANSI C idea: structure literals

William Sommerfeld wesommer at athena.mit.edu
Mon Feb 29 06:43:16 AEST 1988


In article <56 at vsi.UUCP> friedl at 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];
     



More information about the Comp.lang.c mailing list