home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16130 < prev    next >
Encoding:
Text File  |  1992-11-08  |  1.3 KB  |  50 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!netnews!bandy
  3. From: bandy@netnews.jhuapl.edu (Mike Bandy)
  4. Subject: Re: Returned struct
  5. Message-ID: <BxC0uv.H22@netnews.jhuapl.edu>
  6. Organization: JHU/Applied Physics Laboratory
  7. References: <1992Nov5.075611.14809@piccolo.cit.cornell.edu>
  8. Date: Sat, 7 Nov 1992 06:04:07 GMT
  9. Lines: 39
  10.  
  11. sl14@crux3.cit.cornell.edu (Stephen Lee) writes:
  12.  
  13. >How can one make use of a struct returned by a function?  e.g.
  14.  
  15. >struct complex complex_add(struct complex a, struct complex b);
  16.  
  17. >I tried
  18.  
  19. >struct complex z1, z2, z3;
  20.  
  21. >z1.x = 0; z1.y = 3;
  22. >z2.x = 5; z2.y = 4;
  23.  
  24. >z3 = complex_add(z1, z2);
  25.  
  26. >But BC++ 2.0 complained 'illegal structure operation' on the above.
  27. >Note:  I'm using C, not C++.
  28.  
  29. I'm not familar with BC++ but in my old VAX/VMS days I saw this sort of thing
  30. happen when the size of the thing being passed/returned was too big to be
  31. pushed on the stack or loaded into a register or something like that.  The
  32. solution was to pass/return the ADDRESS of the structure.  So I'd change
  33. the routine to look like:
  34.  
  35.   complex_add(struct complex *a, struct complex *b, struct complex *result);
  36.  
  37. Then references to variables withing complex_add() have to change like:
  38.  
  39.   result->x = a->x + b->x;
  40.  
  41. And the call becomes:
  42.  
  43.   complex_add(&z1, &z2, &z3);
  44.  
  45. -- 
  46.  
  47.     Mike Bandy
  48.     bandy@aplcomm.jhuapl.edu
  49.     Johns Hopkins University / Applied Physics Lab
  50.