home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!portal!dfuller
- From: dfuller@portal.hq.videocart.com (Dave Fuller)
- Subject: Re: Returned struct
- Message-ID: <Bx9pJE.Et4@portal.hq.videocart.com>
- Organization: VideOcart Inc.
- X-Newsreader: Tin 1.1 PL3
- References: <1992Nov5.075611.14809@piccolo.cit.cornell.edu>
- Date: Fri, 6 Nov 1992 00:04:26 GMT
- Lines: 45
-
- sl14@crux3.cit.cornell.edu (Stephen Lee) 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);
- :
-
- 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.
-
- try int complex_add(struct complex *a, struct complex *b, struct complex *c)
- then call it as complex_add(&z1, &z2, &z3)
-
- then in it the routine do this
-
- int complex_add(. . .definitions again . . .)
- {
- c->x = a->x + b->x;
- c->y = a->y + b->y;
- .
- .
- or whatever you want to do with the members
- .
-
- return 0;
- }
-
-
- As i said, you can pass structures with some compilers, but if you want
- to do this the 'right' way, pass the pointers to them instead.
-
- Dave Fuller
- dfuller@portal.hq.videocart.com
-