Article View: comp.sys.atari.st
Article #24097Re: Stack and other C variable-storage spaces
From: braner
Date: Thu, 18 Dec 1986 16:09
Date: Thu, 18 Dec 1986 16:09
79 lines
2729 bytes
2729 bytes
[]
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<n; i++) /* loop the right number of times */
x = a[i][j]; /* use a[][] as if it was a 2-d array */
...
}
Later, in a different file:
#include "func.c"
...
main()
{
double *a[7]; /* array of pointers */
double aa[7][7]; /* the physical array of doubles */
for (i=0; i<7; i++)
a[i] = &aa[i][0]; /* have to initialize a[]! */
double *b[5]; /* another one, of different size */
double bb[5][5];
for (i=0; i<5; i++)
b[i] = &bb[i][0];
...
x = func(7,a);
y = func(5,b);
}
The ability to do this (along with the availability of pointers-to-functions)
is a major advantage of C over, say, FORTRAN.
- Moshe Braner
Technical Details
Message-ID:
<1870@batcomputer.tn.cornell.edu>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!seismo!rutgers!mit-eddie!genrad!decvax!tektronix!uw-beaver!cornell!batcomputer!branerReferences:
<1865@batcomputer.tn.cornell.edu>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!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