Path: utzoo!utgpu!water!watmath!clyde!att!whuts!homxb!hound!rkl1 From: rkl1@hound.UUCP (K.LAUX) Newsgroups: comp.lang.c Subject: Re: Portability of passing/operating on structures... Message-ID: <2685@hound.UUCP> Date: 16 Oct 88 17:23:01 GMT References: <8810111934.AA21941@ucbarpa.Berkeley.EDU> Organization: AT&T Bell Laboratories, Holmdel Lines: 33 In article <8810111934.AA21941@ucbarpa.Berkeley.EDU>, U23405@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@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