Variable dimensioning in fortran
Richard Harter
g-rh at cca.CCA.COM
Wed Jun 22 16:33:11 AEST 1988
In article <517 at philmds.UUCP> leo at philmds.UUCP (L.J.M. de Wit) writes:
... Re previous discussion of a proposed substitute for
fortran subscripting
]The above C code is not adequate for handling flat arrays; the 2 * 5
]elements should be allocated as one chunk, with the r[0], r[1], ...
]pointing into it, as I explained in a previous article. I'll repeat
]that code here for clearness (and removed the errors; this one will
]work 8-):
]double **Create2DArray(w,h)
]int w,h;
]{
] double **r, *a;
]
] a = (double *)calloc(w * h, sizeof(double));
] r = (double **)calloc(h,sizeof(double *));
] for ( ; --h >= 0; r[h] = a + w * h) ;
] return r;
]}
]This approach has, besides the array being contiguous, as a benifit
]that only two allocations are needed. The array can be handled both by
]vectoring: r[0], r[1], ... and by using the flat array pointed to by
]r[0]. Now for your example:
Sundry examples omitted. The main point of the examples is
that any static dimensioning desired can be set up by properly casting
pointers. This is quite correct. The thing that cannot be done in C
is to have variable dimensioning using C subscripting (as many people
have pointed out you can fake it with a macro which does the address
calculation.)
Offhand, my feeling is that I wouldn't use any of these schemes.
C isn't fortran and vice versa. C doesn't allow
foo(m,n,a)
int m,n;
double a[m][n];
{....}
and that's the way it is. Cute tricks to simulate this just make the
code obscure.
--
In the fields of Hell where the grass grows high
Are the graves of dreams allowed to die.
Richard Harter, SMDS Inc.
More information about the Comp.lang.c
mailing list