home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!charon.amdahl.com!pacbell.com!decwrl!sdd.hp.com!spool.mu.edu!wupost!uwm.edu!zaphod.mps.ohio-state.edu!uunet.ca!wildcan!sq!msb
- From: msb@sq.sq.com (Mark Brader)
- Subject: Re: Returned struct
- Message-ID: <1992Nov6.041315.17321@sq.sq.com>
- Organization: SoftQuad Inc., Toronto, Canada
- References: <1992Nov5.075611.14809@piccolo.cit.cornell.edu> <Bx9pJE.Et4@portal.hq.videocart.com>
- Date: Fri, 6 Nov 92 04:13:15 GMT
- Lines: 68
-
-
- Stephen Lee (sl14@crux3.cit.cornell.edu) writes:
- > : How can one make use of a struct returned by a function? e.g.
- > :
- > : struct complex complex_add(struct complex a, struct complex b);
- > :
- > : I tried
- > :
- > : struct complex z1, z2, z3;
- > :
- > : z1.x = 0; z1.y = 3;
- > : z2.x = 5; z2.y = 4;
- > :
- > : z3 = complex_add(z1, z2);
-
- This looks correct, but it is not a complete program. The most likely
- cause of the compiler's rejection of the code is that complex_add() was
- actually defined *after* the code that called it, or in a separate file,
- and no declaration of it was provided before the call in the same file
- as the call. If the lines quoted above actually occurred in the same
- file in the order shown, and if struct complex actually contains members
- named x and y whose type is arithmetic, then the problem must lie some-
- where else in the program.
-
- Dave Fuller (dfuller@portal.hq.videocart.com) responds:
-
- > If indeed you want to use a return value, it should exist as a pointer
- > to the structure ... 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.
-
- This reply of Dave's seems to have passed through some sort of time and
- causality warp. Stephen's question uses function prototype syntax, which
- was introduced into C at the time of the ANSI standardization in the late
- 1980s. Dave's article plainly shows by its content that it was written
- much earlier, in the 1970s or early 1980s. Yet it shows up today, in 1992,
- *as* *a* *followup* to Stephen's question. How on Earth is this possible?
- Followups to rec.arts.sf.misc. :-)
-
-
- H'rm.
- The point is that, as the FAQ says (you have read it, Dave?), the passing
- and assigning of structs has been part of C for something like 14 years
- now, and most people now consider it reasonable to assume that they'll
- never again see a compiler that doesn't support it. The earliest C
- compilers didn't support "unsigned short" or "unsigned long" either --
- should we avoid those data types? No; that'd be silly today.
-
- If one is going to criticize Stephen's code for non-portability to older
- implementations, then the one thing that must be seriously mentioned is
- the use of function prototypes. There are still quite a few compilers out
- there that are over 5 years old and which will throw syntax error fits
- when they see
-
- struct complex complex_add(struct complex a, struct complex b);
-
- If the compiler's diagnostic had related to syntax rather than being
- "illegal structure operation", and if the compiler wasn't mentioned as
- being related to a C++ implementation, I would suspect that that was what
- was going on here. As it is, I can't say what is wrong; I can only say
- what is right.
- --
- Mark Brader "Nicely self-consistent. (Pay no attention to
- SoftQuad Inc., Toronto that D-floating number behind the curtain!)"
- utzoo!sq!msb, msb@sq.com -- Chris Torek, on pasta
-
- This article is in the public domain.
-