[comp.lang.c] assigning a structure

anigbogu@loria.crin.fr (Julian Anigbogu) (04/30/90)

In article <21734@dartvax.Dartmouth.EDU> pete@eleazar.dartmouth.edu (Pete Schmitt) writes:
>struct inv {	long number; 	char name[2]; }
>struct inv func(s) , s;
>{
>	struct inv s2;
>	s2 = s;		/* is this legal?  If not why not. If so why so. */
>    	return(s2);	/* it seems to work okay. How do the members get */
>			/* their assignment?  Do pointers come to play here? */
>}

Assigning s to s2 is perfectly legal C so you shouldn't be surprised
that  returning s2 does exactly what you want. The members get their
correct values because this is done in a one-to-one correspondence.
That is why, the necessary condition is that both source and
destination have to be of the same type.

Julian
------
				---------------------------------------
e-mail:	anigbogu@loria.crin.fr 	| All opinions expressed here are      |
				|  naturally mine. However ...         |
				----------------------------------------
-- 
				---------------------------------------
e-mail:	anigbogu@loria.crin.fr 	| All opinions expressed here are      |
				|  naturally mine. However ...         |
				----------------------------------------

eager@ringworld.Eng.Sun.COM (Michael J. Eager) (05/03/90)

In article <21734@dartvax.Dartmouth.EDU> pete@eleazar.dartmouth.edu (Pete Schmitt) writes:
>
>struct inv
>{
>	long number;
>	char name[2];
>}
>struct inv func(s)
>struct inv s;
>{
>	struct inv s2;
>
>	s2 = s;		/* is this legal?  If not why not. If so why so. */
>    	return(s2);	/* it seems to work okay. How do the members get */
>			/* their assignment?  Do pointers come to play here? */
>}
>--
>     Peter Schmitt					UNIX/VMS Consultant
>Kiewit Computation Center				   User Services
>   Dartmouth College					   (603)646-2085
>   Hanover, NH 03755				    Peter.Schmitt@Dartmouth.EDU


The ANSI standard gives "full faith and credit" to structured variables.
They can be assigned, passed to functions and returned from functions exactly
as a simple variable.  Assignment (and by extension argument passing and 
returning) is on a bit-copy basis.  No pointers need be used.

Returning values is a bit sticky to implement, especially with recursive
routines.

-- Mike Eager

roland@cochise.pcs.com (Roland Rambau) (05/07/90)

pete@eleazar.dartmouth.edu (Pete Schmitt) writes:

->struct inv
->{
->	long number;
->	char name[2];
->}
->struct inv func(s)
->struct inv s;
->{
->	struct inv s2;
->	s2 = s;		/* is this legal?  If not why not. If so why so. */

Yes, its legal ( with ANSI ). Its a structure assignment, will copy 
sizeof( struct inv ) bytes of memory ( 6 byte or 8 byte or whatever
the implementor may need for alignment )

->    	return(s2);	/* it seems to work okay. How do the members get */
->			/* their assignment?  Do pointers come to play here? */

Also legal. There are several quite different techniques to implement this;
but since s2 is an auto object, this return statement will probably imply
another 6-8 byte copy im memory.

->}
Roland Rambau

  rra@cochise.pcs.com,   {unido|pyramid}!pcsbst!rra,   2:507/414.2.fidonet