Xref: utzoo comp.lang.fortran:794 comp.lang.c:10858
Path: utzoo!attcan!uunet!husc6!cca!g-rh
From: g-rh@cca.CCA.COM (Richard Harter)
Newsgroups: comp.lang.fortran,comp.lang.c
Subject: Re: Variable dimensioning in fortran
Keywords: language conversions, FORTRAN, c
Message-ID: <29797@cca.CCA.COM>
Date: 22 Jun 88 06:33:11 GMT
References: <2742@utastro.UUCP> <20008@beta.UUCP> <224@raunvis.UUCP> <3415@ut-emx.UUCP> <5917@aw.sei.cmu.edu> <29605@cca.CCA.COM> <517@philmds.UUCP>
Reply-To: g-rh@CCA.CCA.COM.UUCP (Richard Harter)
Organization: Computer Corp. of America, Cambridge, MA
Lines: 50

In article <517@philmds.UUCP> leo@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.