home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!sdrc!thor!scjones
- From: scjones@thor.sdrc.com (Larry Jones)
- Newsgroups: comp.lang.c
- Subject: Re: Returned struct
- Message-ID: <2226@sdrc.COM>
- Date: 6 Nov 92 15:44:13 GMT
- References: <1992Nov5.075611.14809@piccolo.cit.cornell.edu> <Bx9pJE.Et4@portal.hq.videocart.com>
- Sender: news@sdrc.COM
- Lines: 35
-
- In article <Bx9pJE.Et4@portal.hq.videocart.com>, dfuller@portal.hq.videocart.com (Dave Fuller) writes:
- > sl14@crux3.cit.cornell.edu (Stephen Lee) writes:
- > : How can one make use of a struct returned by a function? e.g.
- >
- > If indded you want to use a return value, it should exist as a pointer
- > to the structure (which is either static or malloc'd, both are messy)
- > secondly, passing structures around is not the right way to deal
- > with them, you need to pass them as pointers. Although some compilers
- > allow passing of structures, lets assume it will end up on one that
- > doesn't do this.
-
- Structure assignment, structure arguments, and structure return values
- were added to C in 1978, very shortly after K&R was published. K&R
- even mentions that it is expected that those operations would be added
- in the future. Any compiler you run across today that doesn't support
- them is apt to be either so new and incomplete and it isn't likely to
- be usable, or so old and venerable that it should be allowed to rest
- in peace. In the example Stephen gave (complex arithmetic), using
- struct arguments and return values is exactly the right thing to do
- since it allows you to write expressions like:
-
- d = cmp_sqrt(cmp_add(cmp_mult(a, a), cmp_mult(b, b)));
-
- Granted, that's not as clear as you could do with C++:
-
- d = sqrt(a * a + b * b);
-
- But it's a lot clearer than the mess you'd end up with if you only
- used pointers and had to use explicit temporaries with lots of copying
- between them.
- ----
- Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH 45150-2789 513-576-2070
- larry.jones@sdrc.com or ...uunet!sdrc!larry.jones
- Years from now when I'm successful and happy, ...and he's in
- prison... I hope I'm not too mature to gloat. -- Calvin
-