Is simple assignment allowed with structs

Doug Gwyn gwyn at smoke.brl.mil
Tue Apr 9 07:24:25 AEST 1991


In article <1991Apr8.073710.22673 at netcom.COM> avery at netcom.COM (Avery Colter) writes:
>gwyn at smoke.brl.mil (Doug Gwyn) writes:
>>>I tried adding two structs and that did not work.
>>Of course not.  What would it mean?
>Now, of course, there is nothing stopping you from making a little function
>to "add" two structs of the same type and return a struct of this type
>as the function result; the body of the function would just be field-by-field
>simple addition of the parallel field values in the input structs.

This is not always possible, for example for pointer members.
Even if it were possible, it may not correctly represent an algebraic
"addition" of the quantities represented by the structures, although
often it would.

>The reason assignment works is that structs of the same type are of the same
>size in bytes, and all have their fields at the same byte offsets, so that
>assignment would just mean copying the range of bytes holding the rvalued
>struct into the area holding the lvalued struct.

No, the reason that structure assignment works is that it was decided
that it had clear, universal semantics and that it would frequently be
useful, so the C language was REVISED to require support for structure
assignment.  The implementation must provide the proper semantics, not
the other way around.



More information about the Comp.lang.c mailing list