/* LISTING 6 */

#define DM2init                 struct DM2 \
        { int rows, cols; double * data; }

#define DM2type                 struct DM2

DM2init;

DM2type DM2alloc(DM2type *p, int r, int c) {
    p->rows = r;
    p->cols = c;
    p->data = (double *)
        malloc((unsigned)r*c*sizeof(double));  

    return *p;
}

#define DM2item(x,i,j)          x.data[i*x.cols+j]

void DM2free(DM2type * x) {
    free((void *)x->data);
}

main()
{
    DM2type m;
    int i,j;

    DM2alloc(&m, 7,13);
    for(i=0; i<7; ++i)
        for(j=0; j<13; ++j)
            DM2item(m,i,j) = (double) (i*100)+j;

    for(i=0; i<7; ++i) {
        for(j=0; j<13; ++j)
            printf("%3g ",DM2item(m,i,j) );
        printf("\n");
    }
    DM2free(&m);
}

