Portability of passing/operating on structures...

Bill.Stewart.[ho95c] wcs at skep2.ATT.COM
Wed Oct 19 12:26:33 AEST 1988


In article <8810111934.AA21941 at ucbarpa.Berkeley.EDU> U23405 at UICVM.Berkeley.EDU (Michael J. Steiner) writes:
> Is it considered portable to do the following things with
> structures or unions?

There were a number of additions to the C language in about 1980, after
the original K&R book, which almost everyone has adopted, even most
compilers that call themselves "K&R".  The main features were void,
enums, and structure handling.  Not everybody has them, but the
following things are portable to any useful environment.

>     -pass them (by value) to functions
	Yes.
>     -have functions which return them
	Yes.  Note that the mechanism for returning the value is
	implementation-defined.  Some people put them on the stack;
	others may do arbitrarily strange things.
>     -assign them (=)
	Yes.
>     -test them (structures only) for equality with ==
	No!  The problem is that structures typically have holes in
	them caused by alignment requirements, and these holes have
	arbitrary junk in them.  When you assign one structure to
	another, this junk is generally copied, but if you assign
	values member by member you aren't changing the junk in the
	holes, so equality comparisons don't work.
-- 
#Bill Stewart, AT&T Bell Labs 2G218 Holmdel NJ 201-949-0705 ho95c.att.com!wcs
# New Jersey Division of Motor Vehicles - 
#	rising to a new level of incompetence



More information about the Comp.lang.c mailing list