Portability of passing/operating on structures...

K.LAUX rkl1 at hound.UUCP
Mon Oct 17 03:23:01 AEST 1988


In article <8810111934.AA21941 at ucbarpa.Berkeley.EDU>, U23405 at UICVM.Berkeley.EDU (Michael J. Steiner) writes:
> What I would like to know is (someone asked this before, I think):
>  
> Is it considered portable to do the following things with
> structures or unions?
>     -pass them (by value) to functions
>     -have functions which return them
>     -assign them (=)
>     -test them (structures only) for equality with ==
> 
>                                                  Thanks in advance,
> 
>                                                  Michael Steiner
>                                                  Email: U23405 at UICVM.BITNET


	*Why* would you want to do this anyway?  It is far easier to use
pointers (and much more efficient).  You should pass a *pointer* to the
struct/union, declare functions as *returning a pointer* to the struct/union,
as far as assignment goes, you want a copy of the contents of the struct and
as for testing for equality, how can you assume that different compilers
will evaluate them properly and exactly the same?

	If you are trying to get the compiler to automatically generate code
to copy the fields of the structure, *don't*.  What you can do is declare
a pair of *unsigned char* pointers and cast the addresses of the source and
destination structures to 'unsigned char', then do a short loop to copy byte
by byte based on 'sizeof (struct)'.

	As for testing equality of the contents of two unions/structures,go
through the effort of testing field by field -  you can't go wrong this way.

--rkl



More information about the Comp.lang.c mailing list