ANSI C idea: structure literals
Alan J Rosenthal
flaps at dgp.toronto.edu
Mon Feb 29 04:05:26 AEST 1988
In article <56 at vsi.UUCP> friedl at vsi.UUCP (Stephen J. Friedl) writes:
> struct foo *table[] = {
> & { "Bill", 25 },
> & { "Jim", 10 },
> & { "Bob", 40 },
> };
>
> Where the compiler sees the & before the {initializer}, it puts
>the {struct} somewhere and throws the pointer into the table.
This is good. It is an analogue of what happens with strings. It
would be good to extend this to arrays and other things. For example,
why not be able to write "&3" to get a pointer to int which points to a
3? (This is currently achievable by the non-portable (int *)"\0\0\0\003".)
The basic advantage of this is it reduces the need for temporary
variables, which are generally a Bad Thing. This is not as bad an
addition as some of the ANSI things because there is semi-precedent
with strings.
Unfortunately, this doesn't seem to allow creating, say, a short in
this manner, because you can't specify shorts in expressions. Any
attempt to fix this would probably be a very bad addition (such as
differentiating "&(short)3" from "&3" even though "(short)3" and "3"
usually are identical). (Of course, they're already different when
sizeof is used on them, but this is already ugly.)
ajr
--
If you had eternal life, would you be able to say all the integers?
More information about the Comp.lang.c
mailing list