Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!cornell!uw-beaver!tektronix!nosun!dawson.fpssun.fps.com!dawson From: dawson@fpssun.fps.com (Bryan Dawson ext 1184) Newsgroups: comp.lang.c Subject: Re: Portability of passing/operating on structures... Summary: there are also good reasons for passing and returning structs to/via functions... Message-ID: <342@dawson.fpssun.fps.com> Date: 20 Oct 88 15:05:17 GMT References: <8810111934.AA21941@ucbarpa.Berkeley.EDU> <2685@hound.UUCP> Organization: Floating Point Systems Lines: 60 In article <2685@hound.UUCP>, rkl1@hound.UUCP (K.LAUX) writes: > 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, [ lots of good arguments for using pointers to structs deleted... ] Well, since you *did* ask, there are many very useful algorithms for sorting, searching, building, manipulating and accessing data structures which are *much* easier to use if the atomic 'data item' is passed into a functions *by value* and if functions can also generate *and then return* items of this type. For practical applications, this atomic 'data type' will usually be a structure with several fields. Some of these algorithms are recursive and require a 'new' instance of the structure on every call. This is done much more easily and naturally by passing the prior structure in *by value* modifying the new version and continuing. It is probably for good reasons like this that most good C compilers and the ANSI draft standard allow structures to be passed by value, returned from functions, and assigned as entities. Test for equality is prohibited by ANSI and seldom supported because it is troublesome to implement when the structures in question are padded internally and thus have undefined 'holes' within them which preclude a byte-for-byte test for equality. Please note that I do not disagree that pointers to structures are very natural and useful and can often be a more efficient method for manipulating structures with functions. I am merely pointing out that the use of structures as full-fledged entities sometimes has value which outwieghs the efficiency penalty for such use. It is sometimes important to note (especially in C) that efficiency is only ONE of MANY constraints on good program design. In situations where efficiency is not critical, I'll take readable, maintainable, simple and natural code which clearly follows the chosen algorithm any day... -- ============================================================================ = Bryan Dawson tektronix!fpssun!dawson ...The story you have just = = read is true, reality was subsequently changed to protect the innocent. = ============================================================================