home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19234 < prev    next >
Encoding:
Internet Message Format  |  1993-01-05  |  2.3 KB

  1. Path: sparky!uunet!math.fu-berlin.de!mailgzrz.TU-Berlin.DE!jaenicke
  2. From: jaenicke@emserver.ee.tu-berlin.de (Lutz Jaenicke)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: ** in C
  5. Date: 5 Jan 1993 09:06:26 GMT
  6. Organization: TU-Berlin, Institut fuer Elektrische Maschinen
  7. Lines: 50
  8. Message-ID: <1ibj2iINNt7c@mailgzrz.TU-Berlin.DE>
  9. References: <C0D3xs.1y2@NeoSoft.com>
  10. NNTP-Posting-Host: emws1.ee.tu-berlin.de
  11.  
  12. In article <C0D3xs.1y2@NeoSoft.com> martink@NeoSoft.com (Martin Koistinen) writes:
  13. >I have a function that needs a 2D array. So I declared it as follows:
  14. >double *My_Function(double A[3][3])
  15. >{
  16. >    static double R[3];
  17. >      .
  18. >   return((double *)&R);
  19. >}
  20. >I have a header file that declares it as:
  21. >double *My_Function(double **)
  22. >My code refers to it as:
  23. >double M[3][3];
  24. >double *p;
  25. >p = My_Function(M);
  26. >I have two questions:
  27. >1) Is there any reason why I shouldn't return a pointer to a static from a
  28. >function? I realize that I may get bad results if I try to reference the
  29. >pointeaafter I do another call to the function or try to use it twice in
  30. >the same statement but is there anything else?
  31. If you are aware of the mentioned properties, you may use it. Many system
  32. calls (e.g. getpwuid as mentioned in another thread today) use this
  33. technique, too.
  34.  
  35. BUT: You should have a look at your return statement, you do return
  36. "pointer to an array of double", which is pointer to the pointer to the
  37. first element. Your compiler gives you no warning? Then have a look at your
  38. wrong casting. You want to return "pointer to the first element", which
  39. is
  40.     return R;    /* No casting needed */
  41.  
  42.  
  43. >2) My compiler gives me a warning that I am passing the function something
  44. >other then what it wants. Why?
  45. >My code seems to work great but I feel better when my compiler doesn't
  46.           ^^^^^^^^^^ I doubt this
  47. >give me warnings. It was my understanding that n Dimensional arrays break
  48. >down into pointers (n references).
  49. NO!!! One dimensional arrays are equivalent to pointers.
  50. The pointer<->array equivalence DOESN'T hold for multidimensional arrays.
  51. Please have a look into K&R (II) and the excellent FAQ, where this
  52. problem is disussed.
  53.  
  54.     Lutz
  55. -- 
  56. Lutz Jaenicke                 jaenicke@emserver.ee.tu-berlin.de 
  57. Institut fuer Elektrische Maschinen    jaenicke@emapollo.ee.tu-berlin.de
  58. Technische Universitaet Berlin        Tel. (004930)314-24552
  59. Einsteinufer 11, D-1000 Berlin 10     Fax. (004930)314-21133 
  60.