Re: A[x][y][z]



sterten@xxxxxxx writes:
> what I had in mind and meant with "pre-compiler" or "preprocessor"
> is a small utility which reads the C-"source" containing A[x,y]
> or int A[n] and converts it into another C-source-file
> where A[x,y] is converted into A[x][y] and int A[n]; into
> int A[n+1] and maybe some other things too.
>
> This can't be so difficult to write and probably already
> exists somewhere ?!?

Flash Gordon has already reminded you about the context thing, so I
won't rant about that.

If you really wanted to, you could do something like this:

double A[10][20][30];
#define A_(x,y,z) (A[(x)][(y)][(z)])

You could then write A_(x,y,z) and have it expand to the equivalent of
A[x][y][z], but with extra parentheses to avoid operator precedence
problems.

A problem with this is that the scope of A is the same as for any
object declared at that point, but the scope of the A_ macro goes from
the point of its definition to the end of the file. Also, since
A_(x,y,z) doesn't use square brackets, it doesn't look like an array
indexing operation. A (possibly inline) function with the same
profile won't work because you can't assign to the result. A function
returning a pointer to the array element would require you to
explicitly dereference the pointer whenever you want to use it.
Personally, I find any of these options uglier than A[x][y][z], but
it's your call.

As for wanting arrays to be 1-based, the book _Numerical Recipes in
C_, available at <http://www.library.cornell.edu/nr/bookcpdf.html>,
uses some tricks that do this. I haven't looked at them in detail,
but I suspect they might invoke undefined behavior by using a base
address before the beginning of the array object (which will happen to
work if that base address happens to be within the address space of
the program).

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.