Path: utzoo!attcan!uunet!seismo!sundc!pitstop!sun!quintus!ok From: ok@quintus.uucp (Richard A. O'Keefe) Newsgroups: comp.lang.fortran Subject: Re: RE: An array by any other name. . . Message-ID: <415@quintus.UUCP> Date: 16 Sep 88 10:16:00 GMT References: <994@amelia.nas.nasa.gov> <3641@lanl.gov> Sender: news@quintus.UUCP Reply-To: ok@quintus.UUCP (Richard A. O'Keefe) Organization: Quintus Computer Systems, Inc. Lines: 29 In article <3641@lanl.gov> jlg@lanl.gov (Jim Giles) writes: >Furthermore, in C x[i][j] is _supposed_ to be semantically equivalent >to *(*(x+j)+i). Statically allocated 2-d arrays aren't implemented >this way! Many versions of dynamically allocated 2-d arrays are. x[i][j] *is* equivalent to *(*(x+i)+j). Suppose we have double x[20][30]; then x[i], or equivalently *(x+i) would be of type array 30 of double. This expression is immediately converted to a pointer to its first element (which thus is of type "pointer to double"), and x[i][j] = *(( (double*)& *(x+i) )+j) I just checked with a UNIX C compiler, and given the declaration above, x[i][j] and *(*(x+i)+j) yielded the same compiled code. >In order to get a dynamically allocated to behave semantically like >a statically allocated one I have to use a _different_ declaration >syntax. Nope. double (*y)[20][30]; y = (double (*)[20][30]) malloc(sizeof y); "cdecl" explains this as cast malloc(sizeof y) into pointer to array 20 of array 30 of double Now use it as (*y)[i][j] The only difference is that instead of writing "x" you write "(*y)", and you do that consistently in the declaration and the uses.