Variable dimensioning in fortran (now in C)

Bob Rose rrr at naucse.UUCP
Thu Jun 23 02:53:11 AEST 1988


In article <517 at philmds.UUCP>, leo at philmds.UUCP (Leo de Wit) writes:
> I'll repeat that code here for clearness (and removed the errors;
> this one will work 8-):
> 
> double **Create2DArray(w,h)
> int w,h;
> {
>     double **r, *a;
> 
>     a = (double *)calloc(w * h, sizeof(double));
>     r = (double **)calloc(h,sizeof(double *));
>     for ( ; --h >= 0; r[h] = a + w * h) ;
>     return r;
> }

Close, but ...  I assume you are using calloc to zero the array, but
the whole world is not a VAX. Try:

double **Create2DArray(w,h)
register int w,h;
{
    register double **r, *a, **q;
    register int i;

    if (((a = (double *)malloc(i = w*h, sizeof(double))) == 0) ||
        ((r = (double **)malloc(h,sizeof(double *))) == 0))
            abort();
    /*
     * Zero the array.
     */
    for (*r = a; i--; *a++ = 0.0) ;
    for (q = r, a = *r; --h; *++q = (a += w)) ;
    return r;
}

Just fuel for the fire.
                            -bob



More information about the Comp.lang.c mailing list