[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Need Orca/C help...



>No, a limitation of C is that, when you malloc/calloc, you get a single-
>dimensioned array.  You can approximate it thus:
>
>    static int *array;
>    int array_val(int x, int y) { return (array[x*100+y]); }
>    ...
>
>    main(int argc, char *argv[]) {
>        array = calloc(10000, sizeof(int));
>        ...
>        getvalue = array_val(5, 10);
>        ...
>    }
>
>or you can 
>#define ARRAY(x,y) (array[(x)*100+(y)])
>
>
>-- 
>Donald Tsang         tsang@cs.washington.edu       ...!uw-beaver!june!tsang
 
Uhhh, no - C just gives you a hunk of heap when you allocate memory -
it's not dimensioned in any way. It's the pointer you use to access
the memory that determines the interpretation of the memory.
 
How about something like this:
 
static int *(array[100][100]);
 
void main(int argc, char **argv)
 
{
        array = calloc(1, sizeof(array));
}                                       
 
or if you just want to initialize a preallocated array:
 
static int array[100][100];
 
void main(int argc, char **argv)
 
{
        memset(array, 0, sizeof(array));
}
 
 
I use both techniques a lot under Watcom C, and if you're 
lucky Orca/C won't choke on them.
 
 
Toshi Morita
tm@netcom.com