From: utzoo!decvax!ittvax!swatt
Newsgroups: net.unix-wizards
Title: Re: Why C is not much better than Pascal at times
Article-I.D.: ittvax.443
Posted: Fri Sep 17 21:50:00 1982
Received: Wed Sep 22 08:38:35 1982
References: sri-unix.3318


Regarding:

	double norm (n, matrix)
	double matrix[n][n];	/* ILLEGAL */
	{
		...
	}

This is true, and yes, it is a pain.  HOWEVER since by C rules
on pointer arithmatic, the expression:

	foobar[expr]

where ``foobar'' is an array of any scalar type, is in all cases
equivalent to:

	*(foobar + (expr))

If ``foobar'' is a 2-dimensional array of dimension (range1, range2)
of any scalar type,

	foobar[expr1][expr2]

is in all cases equivalent to:

	*(foobar + (range2 * expr1) + expr2)

The effect you want can be gotten by:

	#define MATRIX(X,Y) matrix[((X)*n) + (Y)]
	double norm (n, matrix)
	register n;
	register double *matrix;  /* It's REALLY a pointer; don't
				   * believe the manuals on
				   * "matrix[]" as function arguments!
				   */
	{
		int i,j;
		double z;

		z = MATRIX(i, j);

It's not as convenient, but it does work and I doubt you lose much
in efficiency over what a Pascal or Fortran compiler could do for
you.

In fact, if you declare two-dimensional arrays like:

	double norm (array)
	double array[][40];
	{

The compiler now has enough information to generate code for
doubly-subscripted expressions directly.  In N-dimensional arrays, the
compiler needs to know the range of the outtermost subscripts.

	- Alan S. Watt