Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84 SMI; site sun.uucp Path: utzoo!decvax!decwrl!sun!guy From: guy@sun.uucp (Guy Harris) Newsgroups: net.lang.f77,net.lang.c Subject: Re: arrays, anyone? Message-ID: <2748@sun.uucp> Date: Tue, 3-Sep-85 00:37:25 EDT Article-I.D.: sun.2748 Posted: Tue Sep 3 00:37:25 1985 Date-Received: Tue, 3-Sep-85 08:24:51 EDT References: <390@ur-helheim.UUCP> Distribution: na Organization: Sun Microsystems, Inc. Lines: 106 Xref: sun net.lang.f77:366 net.lang.c:6042 (net.lang.c added because this provides a small tutorial in one of the least-understood parts of C - arrays) > Given a dimensioned fortran array A(50,300) ... Now I pass the array to a > c program via the argument list. Given the column/row major differences, > I can address the fortran element A(10,100) as *(apntr+9*300+99) and get > the correct answer. 1) This assumes you declared "apntr" as an "float *" - which isn't the correct thing to do. See below. 2) Besides, I think you actually mean *(apntr + 99*50 + 9). In FORTRAN, the rightmost index selects the largest objects, and the leftmost index selects the smallest objects; i.e., stepping the leftmost index by 1 steps from one array member to the next adjacent one in the address space. > The hassle with this is that the dimensionality of the array must be known > to the c program, ie. 300 must be known. (Well, actually, you mean the 50 must be known - see above.) What's so surprising about that? The same would be the case if you passed A to a FORTRAN subroutine or function. You could give the argument to the FORTRAN program variable dimensions, and pass the 50 and 300 as arguments, or you could explicitly declare it as an array with 50 rows of 300 columns. In *either* case, "the dimensionality of the array must be known to the FORTRAN program, i.e., 50 must be known." (The 300 need not be known unless you're doing bounds checking, since it doesn't affect any of the array strides.) > Now, in C 2-d arrays are simply an array of pointers to a linear array. No, they aren't. In C, 2-dimensional arrays are simply arrays of arrays. There are *NO* pointers involved. They are laid out in memory much as FORTRAN arrays are, only in a different order. Arrays in C seem to cause an inordinate amount of confusion. Amazingly enough, the type "pointer to array of 'float'" and "pointer to 'float'" are *NOT* the same type. Declaring "x" as a "pointer to array of 'float'" is done with float (*x)[]; while declaring "x" as a "pointer to 'float'" is done with float *x; (Think about it - the syntax of declarations of data types in C parallels the syntax used to refer to the base object that an object of that data type refers to. If you have a pointer to an array, first you dereference it to get the array, then you subscript the array. Hence, (*x)[12] refers to the 12th element of that array.) The FORTRAN program is passing a pointer to an array of REALs, with 50 rows and 300 columns. The C equivalent of this is a pointer to an array of 300 arrays of 50 "float"s. An array of 50 "float"s: float a[50]; An array of 300 arrays of 50 "float"s: float a[300][50]; A pointer to such an array: float (*a)[300][50]; Now, to reference A(10,100): (*a)[99][9]; (Remember, C and FORTRAN store arrays in different order, so the subscripts and dimensions have to be reversed.) > My question is can I access the fortran array above as a double indirection > + offset, as I would a C array? No, but then you don't access C arrays that way, either. There's no double indirection, since you don't have any pointers to pointers, just a pointer to an array. > Is there any way of accessing the array that does not need to know the > dimensions to get the offset? [Suggestions like 'do it in fortran' are > not needed--thanks!!] No, there is no such way of accessing the array, even in FORTRAN - "do it in FORTRAN" is, indeed, unhelpful, since you can't do it in FORTRAN either. The only thing you *can* do in FORTRAN in these cases which you can't do in C is to declare an array argument with variable dimensions: SUBROUTINE SUB(A, M, N) REAL A(M, N) ... would transliterate into void sub(a, m, n) float (*a)[m][n]; int m, n; { ... which isn't legal C. Such a construct permits you to defer telling "sub" what the dimensions of "a" are until run time, but you still have to tell it the dimensions. Guy Harris