[comp.lang.c] structure constant assignment

paul@cs.utexas.edu (Supoj Sutanthavibul) (10/05/89)

We can always assign one structure variable to another
of the same type (at least on the SUN).  We can also
assign value to individual members of a structure but
it is so cumbersome especially in for large structures.
Initialization of structure variables is allowed.
However assignment of constant to a structure variable
in single assignment expression is not allowed (since
C syntax does nota allow construction of structure constants)

Why?

Does standard C allow assignment of structure constant as
in the following example?
I know that one can write functions to construct structure
constant but I think that is an overkill.

typedef		struct coord {
			int	x, y;
			}
		Coord;

main()
{
	Coord	p, q;

	q = (Coord){ 0, 0 };	<--- this is not allowed
	p = q;			<--- this is allowed.
	}

henry@utzoo.uucp (Henry Spencer) (10/06/89)

In article <6990@cs.utexas.edu> paul@cs.utexas.edu (Supoj Sutanthavibul) writes:
>C syntax does nota allow construction of structure constants)
>Why?

Because.

Sorry, that may sound unhelpful, but that really is the answer.  It's just
never been a feature of C.

>Does standard C allow assignment of structure constant as
>in the following example? ...
>	q = (Coord){ 0, 0 };

No.

There are problems with the design of such a feature, which have been gone
into at some length on this group before.  What is the type of `{ 0, 0 }'?
If the cast is mandatory, then this is the only place in the language where
that's true, so it isn't really a cast but something else.  There are other
ways of handling it... but in general, it's not a feature of ANSI C because
of limited usefulness and lack of implementation experience.

Try this:

main() {
	Coord q;
	static Coord zero = { 0, 0 };	/* this is just an initialization */

	q = zero;			/* this is legal */
}

This won't work for non-constant values, of course.
-- 
Nature is blind; Man is merely |     Henry Spencer at U of Toronto Zoology
shortsighted (and improving).  | uunet!attcan!utzoo!henry henry@zoo.toronto.edu