Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!rutgers!mit-eddie!genrad!decvax!tektronix!uw-beaver!cornell!batcomputer!braner
From: braner@batcomputer.tn.cornell.edu (braner)
Newsgroups: comp.sys.atari.st
Subject: Re: Stack and other C variable-storage spaces
Message-ID: <1870@batcomputer.tn.cornell.edu>
Date: Thu, 18-Dec-86 11:09:31 EST
Article-I.D.: batcompu.1870
Posted: Thu Dec 18 11:09:31 1986
Date-Received: Fri, 19-Dec-86 05:31:16 EST
References: <1865@batcomputer.tn.cornell.edu>
Reply-To: braner@batcomputer.UUCP (braner)
Organization: Theory Center, Cornell University, Ithaca NY
Lines: 79
Summary: Important correction, more hints

[]

Oops... I goofed.

The declaration of the variable 'a' in one of the examples in a previous
posting of mine about C variable storage was wrong!  It said:

A third way to allocate large arrays is via the malloc() function:

	main()
	{
		double **a;
		extern char *malloc();
		a = (double **) malloc(30*30*sizeof(double));
		...
		x = a[i][j];		/* use like any array...	*/
	}

It should have said:

	main()
	{
		double **a;
		extern char *malloc();
		a = (double **) malloc(30*30*sizeof(double));
		...
		x = a[30*i+j];		/*** instead of a[i][j]	***/
	}

Alternatively, and probably better style:

	main()
	{
		double (*a)[30];      /*** a pointer to a 30-long array ***/
			/* (same as "typedef double D30[30]; D30 *a;")	*/
		extern char *malloc();
		a = (double **) malloc(30*30*sizeof(double));
		...
		x = a[i][j];		/* now use like any 2-d array... */
	}

Note that the declaration "double **a;" in the LAST example in the previous
posting IS correct: I actually set up an array of pointers there!  That last
method, as mentioned, is faster, and could be used in the 30-by-30 case too!
It is also helpful in writing library functions which act on 2-d arrays of
a size unknown at compile time:

func(n, a)
	int	n;		/* dimension, determined at run time	*/
	double	**a;		/* a pointer to an array of pointers	*/
{
	for (i=0; i