Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!gatech!hubcap!beede From: beede@hubcap.UUCP (Mike Beede) Newsgroups: comp.lang.c Subject: Re: Two dimensional arrays in C (BLAST!!) Message-ID: <254@hubcap.UUCP> Date: Thu, 2-Jul-87 20:34:46 EDT Article-I.D.: hubcap.254 Posted: Thu Jul 2 20:34:46 1987 Date-Received: Sat, 4-Jul-87 08:29:58 EDT References: <245@hubcap.UUCP] Organization: Clemson University, Clemson, SC Lines: 35 in article <245@hubcap.UUCP], beede@hubcap.UUCP (ME AGAIN) says: ] in article <238@hubcap.UUCP>, beede@hubcap.UUCP (Me) says: ]> #define matrix_el(array,i,j) (array[y*sizeof(double)+j]) ]> ] which of course is completely bogus. I'm not sure _where_ that ] sizeof(double) popped in, but anyway the correct version is ] ] #define matrix_el(array,i,j) (array[i*y+j]) , ] Well--now the macro is correct if you don't use anything but single constants or variables as the indexing arguments. If we do something like matrix_el(A,k+5,m) it expands to (A[k+5*y+m]) which is surely bogus, since this is k+(5*y)+m instead of (k+5)*y+m. The moral of the story, as Mark Brader was kind enough to point out (twice!), is to always parenthesize the arguments of a macro when they occur in the rhs, like the (REALLY) correct version: #define matrix_el(array,i,j) (array[(i)*y+(j)]) , The sad part is, not only did Mark include the correct version in his mail to me (which I discarded), but I myself was once burned by the lack of parenthesis in a macro. Sorry this took so long to iron out. -- Mike Beede UUCP: . . . !gatech!hubcap!beede Computer Science Dept. ARPA: BEEDE@TECNET-CLEMSON.ARPA Clemson University INET: beede@hubcap.clemson.edu Clemson SC 29631-1906 PH: (803)656-{2845,3444}