Passing 2-D Arrays of unknown size ?

Martin Weitzel martin at mwtech.UUCP
Thu Dec 21 07:28:22 AEST 1989


In article <3180 at uceng.UC.EDU> mfinegan at uceng.UC.EDU (michael k finegan) writes:
[some lines deleted]
-    Is there a method for passing/using multi-D arrays in subroutines,
[most of examples deleted]
-solution_func3(array,rows,cols)
-char *array;
-int rows, cols;
-{
-    int i,j;
-
-   /* But now I can't use array[][] notation,
-    * and must calculate the indices myself !
-    */
-    for(i ...)
-        for(j ...)
-            some_operation(array[i*cols + j]);
				  ^^^^^^^^^
Exactly this is, what a compiler would have to - and can not do
at compile time, because cols is only know at runtime. You
cannot have the array[][] notatation, but you could write a
preprocessor makro:
#define ARR(x,y) (array[(x)*cols+(y)]) to make the following
a (little) more readable:
-
-   /* with typically: array[(long calculated value)*stride*cols +
-    *					stride*(another calculated value)]
-    */
ARR((long calculated value)*stride, stride*(another calculated value)).

Of course, you could consider a switch to C++, where you could overload
the []-Operator ...

-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.lang.c mailing list