ftp.nice.ch/peanuts/GeneralData/Usenet/news/1991/CSNProg-91.tar.gz#/comp-sys-next-programmer/1991/Sep/Passing-2-dimensional-array-as-method-parameter

This is Passing-2-dimensional-array-as-method-parameter in view mode; [Up]


Date: Sun 27-Sep-1991 15:24:58 From: fasano@unix.cis.pitt.edu (Cathy Fasano) Subject: Passing 2-dimensional array as method parameter I'm trying to pass a two-dimensional array as a parameter in a method call. Using the following syntax: in the .h file: - recalcMatrices:(double)matrix1[][9] :(double)matrix2[][9]; in the .m file: - recalcMatrices:(double)matrix1[][9] :(double)matrix2[][9] { int i,j,indx,len; double child1[6][9],child2[6][9]; if(<this is a leaf node>) for(i = 0;i < 6; ++i) for(j = 0; j < 9; ++j) { matrix1[i][j] = ....; matrix2[i][j] = ....; } else { for(i = 0;i < 6; ++i) for(j = 0; j < 9; ++j) matrix1[i][j] = matrix2[i][j] = 0.0; len = [childrenList count]; for(indx = 0; indx < len; ++indx) { [[childrenList objectAt:indx] recalcMatrices:child1 :child2]; for(i = 0; i < 6; ++i) for(j = 0; j < 9; ++j) { matrix2[i][j] += child1[i][j]; matrix2[i][j] += child2[i][j]; } } } } I get a compiler complaint of: .h:36: parse error before `[' .m: In method `recalcMatrices:' .m:115: parse error before `[' and then matrix2 isn't declared... I also tried: - recalcMatrices:(double)matrix1[6][9] :(double)matrix2[6][9]; same complaint about the '[' - recalcMatrices:(double)(*matrix1)[9] :(double)(*matrix2)[9]; complained about the '(' and the '[' - recalcMatrices:(double *)matrix1 :(double *)matrix2; compiler liked this, but then wouldn't let me call it with child1 and child2 as parameters. Does anybody know the correct syntax? I'm using the GNU-NeXT compiler on the NeXT. cathy :-)
Date: Sun 28-Sep-1991 20:29:14 From: seeger%oceania@uunet.UU.NET (Seeger Fisher) Subject: Re: Passing 2-dimensional array as method parameter In article <182871@unix.cis.pitt.edu> fasano@unix.cis.pitt.edu (Cathy Fasano) > I'm trying to pass a two-dimensional array as a parameter in a method call. > Using the following syntax: > in the .h file: > - recalcMatrices:(double)matrix1[][9] :(double)matrix2[][9]; > Does anybody know the correct syntax? > > cathy :-) > "If there's no solution, then it isn't a problem." -- Evening Shade It looks like you are passing a (double **) to me. Try: - recalcMatrices:(double **)matrix1 :(double **)matrix2; Unfortunately, this won't let the compiler check to maker sure that your second dimension is 9; but it might be better than nothing. It might be possible, but I doubt it, considering method syntax (esp. "["'s) to do this: - recalcMatrices:(double *)matrix1[9] :(double *)matrix2[9]; ++seeger
Date: Sun 29-Sep-1991 17:20:40 From: steve@tweedledee.ucsb.edu (Steve Trainoff) Subject: Re: Passing 2-dimensional array as method parameter In article <1991Sep28.202914.3946@oceania.UUCP> seeger%oceania@uunet.UU.NET (Seeger Fisher) writes: > In article <182871@unix.cis.pitt.edu> fasano@unix.cis.pitt.edu (Cathy Fasano) > writes: > > I'm trying to pass a two-dimensional array as a parameter in a method call. > > Using the following syntax: > > in the .h file: > > - recalcMatrices:(double)matrix1[][9] :(double)matrix2[][9]; > > Does anybody know the correct syntax? > It looks like you are passing a (double **) to me. Try: > - recalcMatrices:(double **)matrix1 :(double **)matrix2; > Unfortunately, this won't let the compiler check to maker sure that your > second dimension is 9; but it might be better than nothing. > It might be possible, but I doubt it, considering method syntax (esp. "["'s) > to do this: > - recalcMatrices:(double *)matrix1[9] :(double *)matrix2[9]; These are both wrong. The answer to the original question is that one should use the syntax of C casting. So to tell the compiler that you are passing a pointer to an array with horizontal dimension=9 (and vertical dimension unspecified), you would use: - recalcMatrices:(double (*)[9])matrix1 :(double (*)[9])matrix2; Seeger Fisher's definition doesn't allow one to use matrix[i][j] in the recalcMatrices method since the array really is square; it isn't an array of pointers. It is amazing how many professionals are confused by C casting syntax.

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Marcel Waldvogel and Netfuture.ch.