guy@sun.uucp (Guy Harris) (09/03/85)
(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
roy@phri.UUCP (Roy Smith) (09/06/85)
> > Now, in C 2-d arrays are simply an array of pointers to a linear array. > > Slight misconception here. In C, multi-dimensional arrays are *not* > arrays of pointers to linear arrays. Ah, but they could be if you were willing to do all the memory allocation and pointer set-up yourself. I was and I did. I posted a little routine called arrayalloc to mod.sources a month or so back which does just what you are talking about here. If you are interested, look through your archives for the following or write me and I'll send you a copy. Subject: run-time memory allocation for multi-dimensional arrays Message-ID: <1010@genrad.UUCP> Date: 13 Aug 85 11:59:49 GMT Mod.sources: Volume 2, Issue 36 Submitted by: decvax!allegra!phri!roy (Roy Smith) -- Roy Smith <allegra!phri!roy> System Administrator, Public Health Research Institute 455 First Avenue, New York, NY 10016
guy@sun.uucp (Guy Harris) (09/10/85)
> > > Now, in C 2-d arrays are simply an array of pointers to a linear array. > > > > Slight misconception here. In C, multi-dimensional arrays are *not* > > arrays of pointers to linear arrays. > > Ah, but they could be if you were willing to do all the memory > allocation and pointer set-up yourself. In which case you'd have an array of pointers to linear arrays, not a multi-dimensional array. Guy Harris
phillips@cisden.UUCP (Tom Phillips) (09/11/85)
In article <2748@sun.uucp> guy@sun.uucp (Guy Harris) writes: > [Lots of stuff] >Now, to reference A(10,100): > > (*a)[99][9]; > > ... > Guy Harris WRONG! Declaring an array [99] means the valid values are from 0 -> 98 . See K&R page 20. Tommy Phillips trantor!phillips
gwyn@brl-tgr.ARPA (Doug Gwyn <gwyn>) (09/13/85)
> In article <2748@sun.uucp> guy@sun.uucp (Guy Harris) writes: > > [Lots of stuff] > >Now, to reference A(10,100): > > > > (*a)[99][9]; > > > > ... > > Guy Harris > > WRONG! > Declaring an array [99] means the valid values are from 0 -> 98 . > See K&R page 20. There sure are a lot of turkeys "contributing" to this newsgroup. Guy Harris was right. Don't they teach reading in school these days?
guy@sun.uucp (Guy Harris) (09/16/85)
> >Now, to reference A(10,100): > > > > (*a)[99][9]; > WRONG! > Declaring an array [99] means the valid values are from 0 -> 98 . > See K&R page 20. WRONG! The above code fragment is a reference, not a declaration (the word "reference" in the first sentence might give you a clue here). If you check the original article, you'll notice that the array was declared as having more than 99 rows and having more than 9 elements in each row. As such, the valid subscript values definitely include 99 for the first subscript and 9 for the second. Guy Harris