Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!rutgers!sri-spam!mordor!lll-tis!ames!amdcad!sun!aeras!dgcad!catsim!altnet!altos86!nate From: nate@altos86.UUCP (Nathaniel Ingersoll) Newsgroups: comp.lang.c Subject: Re: Two dimensional arrays in C Message-ID: <344@altos86.UUCP> Date: Wed, 24-Jun-87 12:13:43 EDT Article-I.D.: altos86.344 Posted: Wed Jun 24 12:13:43 1987 Date-Received: Sat, 27-Jun-87 13:59:13 EDT References: <733@cod.UUCP> Reply-To: nate@altos86.UUCP (Nathaniel Ingersoll) Organization: Altos Computer Systems, San Jose, CA Lines: 56 Keywords: ? In article <733@cod.UUCP> murphys@cod.UUCP (Steven P. Murphy) writes: ...declarations... >static double data1[ROWS][COLUMNS]; >static double data2[ROWS][COLUMNS]; make that static double data1[ROWS1][COLUMNS1]; static double data2[ROWS2][COLUMNS2]; > >int n, m; > >n1 = 300; >m1 = 15; > . >matrix_stuff(n1, m1, data1, ROWS1, COLUMNS1); > . >matrix_stuff(n2, m2, data2, ROWS2, COLUMNS2); >} > >matrix_stuff(a, b, array, x, y); >int a, b, x, y; >double array[x][y]; >{ >int i, j; > . >for(i = 0;i <= a;i++) /* step through the rows */ > for(j = 0;j <= b;j++) /* step through the columns */ The problem here is with the double array[x][y]; declaration. You need to declare all the sizes of an arrays dimensions except the first; and this declaration must be _constant_. To do what you want you would probably have to do something like /* * note that the x array bound is unneeded * * various purists will probably be offended by the * following declaration for "array" */ matrix_stuff(a, b, array, x, y) int a, b, x, y; double *array; { #define place(u, v) ((u)*y + (v)) int i, j; /* following with your example, though you'd probably want to eliminate the a and b parameters and use x and y instead */ for (i = 0;i < a;i++) for (j = 0;j < b;j++) /* ...use array[place(i, j)] in here.... for example, this would mutiply all elements of array by 2: array[place(i, j)] *= 2; */ }