delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2004/02/04/04:50:13

X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f
Lines: 55
X-Admin: news AT aol DOT com
From: sterten AT aol DOT com (Sterten)
Newsgroups: comp.os.msdos.djgpp
Date: 04 Feb 2004 08:50:22 GMT
References: <uy8rj5r98 DOT fsf AT elta DOT co DOT il>
Organization: AOL Bertelsmann Online GmbH & Co. KG http://www.germany.aol.com
Subject: Re: array indices [i][j]
Message-ID: <20040204035022.22737.00000765@mb-m19.aol.com>
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Eli wrote:

 >> I would like to use A[i,j,k]  instead of A[i][j][k]
 >
 >You can't really, not with the effect you want it to make.
 >
 >> (I haven't yet figured out what exactly goes wrong, just changed all
 >> ][ to ,   and it compiles but gives strange results)
 >
 >In C, "i, j, k" is a valid expression: it means "take the value of i,
 >discard it, take the value of j, discard it, take the value of k".  So
 >A[i, j, k] is the same as simply A[k], probably not what you want.

why should someone want to take a value - and then discard it ?
I mean, if this A[i,j,k] isn't really needed, then can't we
define it to be A[i][j][k] ?

 >> What's the difference between the notations ?
 >
 >The only correct way to reference a 3-dimensional array in C is
 >A[i][j][k].  If you _must_ use the [i, j, k] notation, the closest

I needn't, but it's easier and usual in mathematics.
You'll hardly find a math-book or paper with this [][] notation !

 >you can get to it is by defining a macro:
 >
 >  #define MA(i,j,k) A[i][j][k]
 >
 >(note the use of parentheses instead of square brackets), or a similar
 >definition of an inline function.

very good ! I wasn't very familiar with macros. Now I see its merits
and coded like this :

// latin cubes
#define nmax 37
#define NL printf("\n");
#define N(i) (i%n+n)%n 
// BTW. pity IMO that e.g. (-5)%3 is -2 and not +1 
#define A(i,j,k) MA[(i%n+n)%n][(j%n+n)%n][(k%n+n)%n]
#define For(i,j,k) for(i=0;i<n;i++)for(j=0;j<n;j++)for(k=0;k<n;k++)
#define FOR(i,j,k) for(i=0;i<n;i++){for(j=0;j<n;j++){for(k=0;k<n;k++){
int MA[nmax][nmax][nmax];
int i,j,k,a,n,x,y,z;

main(){
 n=7;
 For(x,y,z)A(x,y,z)=N(x+2*y-3*z);
 // ...
 FOR(i,j,k)printf("%i ",A(i,j,k));}NL}NL}
}


// Guenter.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019