[comp.lang.c] structure assignment, why you would want to do it

demon@desire.wright.edu (08/25/90)

In article <3615@goanna.cs.rmit.oz.au>, ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
> In article <352@saxony.pa.reuter.COM>, dgil@pa.reuter.COM (Dave Gillett) writes:
> : In <1081.26d26274@desire.wright.edu> demon@desire.wright.edu writes:
> : >struct_thing -= still_more_struct;
> : >struct_thing *= even_more_struct;
> : 
> :      Recall that "x -= y" is equivalent to "x = x - y".  Will your VAX C let
> : you get away with "struct_thing = struct_thing - still_more_struct"?  How
> : have you declared "struct_thing" that lets it both (a) be a structure, and 
> : (b) be in the domain of subtraction, which normally works on *numbers*???
> 
> Let that stand as typical of the answers.
> 

..COBOL definitions/examples removed

> and so on will do the trick.  Given the equivalent of
> 	struct { int a, b; float c; } x;
> 	struct { int b, c, d; } y;
> the COBOL equivalent of x += y would be
> 	ADD CORRESPONDING y TO x.
> which would do
>     /*	x.a : no change, as there is no y.a field */
> 	x.b += y.b;
> 	x.c += y.c;	/* only the names have to match */
>     /*  y.d : not used, as there is no x.d field */
> 
> As it happens, this facility is not present in C.  There is nothing in
> the language which would make it especially difficult to implement, it's
> just that nobody ever thought it worth while.  It was a sensible question.
> 
	Thanx!  Everyone wondered why I asked it.  If you are doing
transformations/similar math stuff or (like I'm doing) vehicle position updates
for a military simulation, you'd like to be able to say

pres_location += current_move;

to update the vehicles location.  I could go through and assign the x,y,z
locations individually, but it would be nice (and cleaner looking) to do a
structure assignment.
        It was intended that the assignement/operator would only be performed
on like structure elements, and only on elements defined for the operater(s).

        Hmm, the next C standard is how many years away? :)

Brett
demon@wsu.bitnet

browns@iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) (08/27/90)

In article <1096.26d52ea3@desire.wright.edu>, demon@desire.wright.edu writes:
 
> 	Thanx!  Everyone wondered why I asked it.  If you are doing
> transformations/similar math stuff or (like I'm doing) vehicle position updates
> for a military simulation, you'd like to be able to say
> 
> pres_location += current_move;
[For those who may not be in on the beginning of the thread, pres_location
and current_move are structure variables, either of the same type or with
matching members.]
> 
> to update the vehicles location.  I could go through and assign the x,y,z
> locations individually, but it would be nice (and cleaner looking) to do a
> structure assignment.
>         It was intended that the assignement/operator would only be performed
> on like structure elements, and only on elements defined for the operater(s).
> 
>         Hmm, the next C standard is how many years away? :)

But you can do this noww, can't you?  Isn't this the sort of "operator
overloading" that is a strength of C++?  I can't give specifics, because
I'm just getting into C++, but seems to me like the problem you're trying
to solve would go better in C++ than C.

Stan Brown, Oak Road Systems, Cleveland, Ohio, U.S.A.         (216) 371-0043
The opinions expressed are mine. Mine alone!  Nobody else is responsible for
them or even endorses them--except my cat Dexter, and he signed the power of
attorney only under my threat to cut off his Cat Chow!

jensting@skinfaxe.diku.dk (Jens Tingleff) (08/27/90)

demon@desire.wright.edu writes:

>In article <3615@goanna.cs.rmit.oz.au>, ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
>> In article <352@saxony.pa.reuter.COM>, dgil@pa.reuter.COM (Dave Gillett) writes:
>> : In <1081.26d26274@desire.wright.edu> demon@desire.wright.edu writes:
[...]
>> 
>	Thanx!  Everyone wondered why I asked it.  If you are doing
>transformations/similar math stuff or (like I'm doing) vehicle position updates
>for a military simulation, you'd like to be able to say

>pres_location += current_move;

Yes, certainly, what next ? multiplication ? (I hear that FORTRAN {8|9}x
got this one "wrong", i.e. a matrix multiplication is an element by 
element multiplication, not the 'lin. algebra' multiplication).

The point is that the semantics become non intuitive after a while.
Anyway, what's wrong with C++ ?


	Jens

Jens Tingleff MSc EE, Institute of Computer Science, Copenhagen University
Snail mail: DIKU Universitetsparken 1 DK2100 KBH O
"It never runs around here; it just comes crashing down"
	apologies to  Dire Straits 

rwelch@diana.cair.du.edu (RANDY S WELCH) (08/27/90)

In article <1096.26d52ea3@desire.wright.edu> demon@desire.wright.edu writes:
[ lots of stuff deleted]

  Thanx!  Everyone wondered why I asked it.  If you are doing
  transformations/similar math stuff or (like I'm doing) vehicle position updates
  for a military simulation, you'd like to be able to say

   pres_location += current_move;

C++ lets you do things like this with structures.  Granted you have to
define the +=, etc operation for it...

-- 
Randy Welch   Mail to :  ...!ncar!scicom!bldr!randy or rwelch@du.edu
Boulder, CO   VOICE   :  303-442-6717
"Unfortunately, life contains an unavoidable element of unpredictability"
-David Lynch "The Angriest Dog in the World"

dgil@pa.reuter.COM (Dave Gillett) (08/28/90)

In <1096.26d52ea3@desire.wright.edu> demon@desire.wright.edu writes:

>In article <3615@goanna.cs.rmit.oz.au>, ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
[[ whole mess of stuff deleted ]]

>	Thanx!  Everyone wondered why I asked it.  If you are doing
>transformations/similar math stuff or (like I'm doing) vehicle position updates
>for a military simulation, you'd like to be able to say

>pres_location += current_move;

>to update the vehicles location.  I could go through and assign the x,y,z
>locations individually, but it would be nice (and cleaner looking) to do a
>structure assignment.
>        It was intended that the assignement/operator would only be performed
>on like structure elements, and only on elements defined for the operater(s).

Hmmmm....  Richard suggests COBOL semantics, and Brent wants something much
closer to APL semantics (which I'll grant is cheaper to implement, although I
still don't know what the hell either of them expects unions to do...).
Brent's description would *at least* allow us to insist that the structures
be of the same type (APL would allow any conformable type, but that's a bit
much to ask of a compiler) and so corresponding structure elements would
only *happen* to have the same name.

I wonder how often people would try to use this to update some components and
not others.  How often would people forget that arithmetic on chars is legal, 
and turn out to be modifying values they meant to leave unchanged.  How smart
an optimizer would it take to roll up the unrolled loop generated by two
structs that happened to be composed of many ints?

And how often would people discover that "a+a" took an order of magnitude
longer than "b+b" because "a" happened to be a struct?

Brent's intended semantics are almost feasible and almost useful.  Almost...
                                             Dave

mat@mole-end.UUCP (Mark A Terribile) (08/30/90)

>> :      Recall that "x -= y" is equivalent to "x = x - y".  Will your VAX C let
>> : you get away with "struct_thing = struct_thing - still_more_struct"?  How
>> : have you declared "struct_thing" that lets it both (a) be a structure, and 
>> : (b) be in the domain of subtraction, which normally works on *numbers*???

In other words, you want to define  operations  +  and  -  on struct types
or your own design.

> > As it happens, this facility is not present in C.  There is nothing in
> > the language which would make it especially difficult to implement, it's
> > just that nobody ever thought it worth while.  It was a sensible question.

Weelll ... what happens when you have stored in that struct not only
coordinates but also data that are not to be added, subtracted, &c?  Like,
for instance, a name string?  There's more to be done than the compiler
can do automatically.

> 	Thanx!  Everyone wondered why I asked it.  If you are doing
> transformations/similar math stuff or (like I'm doing) vehicle position
> updates for a military simulation, you'd like to be able to say
> 
> pres_location += current_move;
> 
> to update the vehicles location.  I could go through and assign the x,y,z
> locations individually, but it would be nice (and cleaner looking) to do a
> structure assignment.
>         It was intended that the assignement/operator would only be performed
> on like structure elements, and only on elements defined for the operater(s).

Well, OK, but what if your coordinates are polar?  Or direction cosines?
Or ... ?

I still think that you need a way to tell the compiler what the operators
mean when applied to these structs of yours.

>         Hmm, the next C standard is how many years away? :)

C is probably the wrong place to look for this stuff.  Expect new C standards
to be clarifications and tweaks and numerical stuff, and maybe conformant
array parameters.

The place to look (as if everyone hadn't by now guessed it) is C++, which
allows you to program just exactly what you have asked for here.  Yes,
really, even including the  +  and  -  operators.  And it's not years away,
it's here NOW.  Don't expect to get things for free; the step from C to
C++ is not a simple one.  If I have you in a classroom, we will spend 50
hours doing it, and you will spend at least 100 hours on assignments to
master the core of the language.

What your asking for isn't simple.  You can get it, but there is a price to
be paid.
-- 

 (This man's opinions are his own.)
 From mole-end				Mark Terribile