Portability of passing/operating on structures...

Guy Harris guy at auspex.UUCP
Thu Oct 20 02:39:50 AEST 1988


>	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)'.

Which may be slower than the code generated by the compiler to copy the
structure....

Passing structures as arguments, returning them as function values, and
assigning structure values to other structures is part of the C language
specified by current ANSI C drafts (if it disappears in the final ANSI C
standard, I'd be damn surprised).  Some older compilers may not handle
it, but most modern compilers should.

(Note that there are compilers that support structure assignment but
don't support "unsigned char"....)

Even if you insist on not doing structure assignments, you may be better
off using your C library's byte-copy routine, if it has one ("memcpy" or
maybe "bcopy") than copying it yourself, byte by byte.  "memcpy" and
"bcopy" are usually optimized to run faster than a naive byte-by-byte copy.

>	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.

Especially since few, if any, compilers implement testing equality of
structures.



More information about the Comp.lang.c mailing list