ANSI C idea: structure literals

Stephen J. Friedl friedl at vsi.UUCP
Sun Feb 28 15:11:54 AEST 1988


Netlanders,

     I've had an idea for C for a long time and would like to see
what you people think of it and whether it is worthy of submis-
sion to the standardardization committee.  I ran this by Doug
Gwyn and he seems to think it's plausible.  Your comments -- pro
or con -- would really be appreciated.  It no strong objections
are raised I'll submit it to the committee.

     This new item might be called "structure literals" (thanks
to Doug for the name).  Basically, let's say that I want to have
a structure foo:

        struct foo {
                char    *name;
                int     age;
        };

     Initializing a table of these is obvious, but what about
statically initializing a table of *pointers* to structures?  I
need to have two tables, the first of which is probably hidden
from the rest of the world.

        struct foo      _table[] = {
                { "Bill",  25 },
                { "Jim",   10 },
                { "Bob",   40 }
        };

        struct foo      *table[] = {
                &_table[0],
                &_table[1],
                &_table[2],
        };

     This is really a bummer to maintain without special kinds of
preprocessors and it strikes me as something that the compiler
should be able to handle in a straightforward manner.  I propose
something like:

        struct foo      *table[] = {
                & { "Bill",  25 },
                & { "Jim",   10 },
                & { "Bob",   40 },
        };

     Where the compiler sees the & before the {initializer}, puts
the {struct} somewhere and throws the pointer into the table.
Note: The & { } notation is just something I thought up off the
top of my head -- alternate ideas are welcome.

     I hope I am not name-dropping or putting Doug on the spot,
but he had some really good ideas on this:

        "I would suggest generalizing the notion to include
        structure literals as the source for assignment state-
        ments, too, which would probably be best done by adding
        'struct-literal' to the a new section 'Structure
        literals' inserted after 3.1.4 ('String literals') that
        describes the syntax and semantics of struct-literal
        (much like the syntax of the { ... } part of a struct de-
        clarator, except contain only constants, string literals,
        and struct literals)."
	[end comments from Doug]

     I guess the general mechanism would allow:

        main()
        {
        struct foo   x, *xp;

                x  = { "Bill", 12 };
                xp = & { "Bill", 12 };

     This undoubtedly bring up a whole host of questions: can we
pass structure-literals as arguments to functions?  Can we cast
them?  Return them from functions?  There are probably lots more.

     I don't have a copy of the current draft but will be picking
one up on Monday and seeing where these all fit in.  I would
really like it if you wizards could comment on this idea: would
it be helpful?  Can it be specified in the language cleanly?  Can
a compiler handle it cleanly?

     Thanks much,
     Steve

-- 
Life : Stephen J. Friedl @ V-Systems, Inc/Santa Ana, CA    *Hi Mom*
CSNet: friedl%vsi.uucp at kent.edu  ARPA: friedl%vsi.uucp at uunet.uu.net
uucp : {kentvax, uunet, attmail, ihnp4!amdcad!uport}!vsi!friedl



More information about the Comp.lang.c mailing list