YA Language Corner
darryl at ISM780.UUCP
darryl at ISM780.UUCP
Thu Oct 31 12:22:00 AEST 1985
Hello, all you budding language laywers out in netland! Today's topic
is struct assignments. The question is, what do they mean? Consider the
following code fragment:
struct {int x;} foo, bar;
f(){
int a,b,c;
a = b * c + ( foo = bar ).x;
}
First off, is this even legal? Well, K&R doesn't discuss struct assignment,
and I don't happen to have the "recent changes" with me. But, reading
the April ANSI Draft, on page 29 we are told that the semantics of
structure members are:
" A postfix expression followed by a dot . and an identifier designates
a member of a structure or union object. The first expression must
evaluate to a structure or union object, and the identifier must name
a member of that type object."
Back on page 27, we learn that a postfix expression is defined as, amongst
other things, a primary expression; in turn, a primary expression can
be a parenthesized expression (page 26); an expression includes simple
assignment.
Page 37, under Assignment Operators, clearly states that "An assignment
operator stores a value in the object specified by the lvalue. An assignment
expression has the type of the left operand and the value of the left
operand after the assignment." This says that the result of the struct
assignment is a value that is a struct and has the value of foo. That
satisfies the first clause above; the second is obviously fulfilled by x.
Why do I bring this up? Well, a different arbiter, our PCC compiler,
disagrees with my interpretation of the language. This is curious,
because it allows constructs such as
g( foo=bar ) and (with suitable declaration) h().x
So, now you decide: Is it a bug? In the compiler or the example?
Just another corner in the language, or obviously a legal construct?
And how much would you enjoy porting legal ANSI C to your crufty old
compiler? Cheers,
--Darryl Richman, INTERACTIVE Systems Corp.
...!cca!ima!ism780!darryl
The views expressed above are my opinions only.
More information about the Comp.lang.c
mailing list