[net.unix-wizards] Why Watt is not much better than ...

swatt (09/27/82)

	From decvax!ucbvax!mark.umcp-cs@UDel-Relay Mon Sep 27 00:41:47 1982
	Date:     25 Sep 82 0:54:15-EDT (Sat)
	From: Mark Weiser <ucbvax!mark.umcp-cs@UDel-Relay>
	Subject:  Why Watt is not much better than... (at times)
	Message-Id: <8208270307.11478@UCBVAX.BERKELEY.ARPA>
	Received: by UCBVAX.BERKELEY.ARPA (3.207 [9/26/82])
		id A11478; 26-Sep-82 20:07:58-PDT (Sun)
	To: unix-wizards@Sri-Unix
	Cc: decvax!ittvax!swatt@Ucb-C70
	Via:  UMCP-CS; 25 Sep 82 2:59-EDT
	
	About the advice recently seen here about how to do double subscripting
	trickily: it doesn't work.  I copied the following program straight
	off the mail.  Is my C compiler funny or what (Berkeley 4.1)?
	The output is given at the bottom.
	#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;
	
			MATRIX(0, 0) = 3.14159;
		}
		main()
		{
			double m[10][10];
			norm(10,m);
			m[1][1] = 3.1415926;
			printf("%7.2f   %7.2f\n",m[0,0],m[1,1]);
		}
		
			
	
	OUTPUT:
	-375522559659364100000000000.000000    3.14
	
	
	
I hope this gets back to you;  I've  had  trouble  with  internet
replies before.  

You  are  mixing  Pascal  and  C  subscripting  conventions.    A
two-dimensional  array  in  C  is  subscripted  "a[e1][e2]",  NOT
"a[e1,e2]" as your example  shows.   Your  example  is  perfectly
legal:  the  ','  operator  means  "evaluate  expression on left;
discard; evaluate expression on right, which is the value of  the
expression as a whole".  Thus "m[0,0]" means evaluate 0 (not hard 
to do); discard; evaluate 0 again (no harder second time around), 
and  this  is  the  value  of  the subscript expression.  Under C
conversion  rules  "m[0]"  is  a  pointer  which  is   equal   to
"&m[0][0]".   What  you  passed  on  the stack to "printf" was an
address; the resulting interpretation as a floating point  number
is justifiably garbage (I know;  Pascal wouldn't let you mismatch 
parameter types on function calls).  

With the offending line in your example changed to:

	printf("%7.2f   %7.2f\n",m[0][0],m[1][1]);

The output is now:

	3.14      3.14

I was not trying to put down Pascal; I was just trying  to  offer
some advice to people like yourself who are interested in getting 
certain  kinds  of  jobs done with a minimum of interference from
the programming language involved.  I don't regard the techniques 
offered as "tricky", since  they  rely  not  on  details  of  any
particular  implementation  of  the  language,  but  rules of the
language itself.  

Granted, it is  not  as  nice  as  a  language  like  Pascal  (or
Fortran),  which  allows  you  to  declare array bounds passed to
procedures as variables, and you have to be careful to understand 
what you're doing with more than two dimensions, but it DOES  get
the job done.  Isn't that what it all comes down to?  

	- Alan S. Watt