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