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

Re: Need Orca/C help...



In article <eg58451@pro-scat.cts.com> seane@pro-scat.cts.com (Sean Eller) writes:
>I'm working on a project that uses arrays and I wanted to clear the array
>to zero after intializing it.  So this is the code I was using:
>int array[100] [100];
>unsigned x;
>unsigned y;
>
>for (y = 0; y < 100; ++y)
>        {
>        for (x = 0; x < 100; ++x)
>                array[y][x] = 0;
>        {
>

Why don't you use "memset(array, 0, 10000 * sizeof(int));"?

>It works fine but takes forever.  I looked in the manual and it says I can
>use the function calloc to do the same thing and it will clear the memory
>with zeros automatically.  My question is:  How would I use the above
>example with calloc and then access the array at a later time?  I think it
>uses pointers but I'm not sure of how to write or read from the array. 
>Before if I wanted to write or read it would simply be this:
>array[5][10] = 1
>or getvalue = array[5][10]

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

       A radioactive cat has 18 half-lives   -- [/usr/games/fortune]