Calling real c-men...(supplement)

Jit Keong Tan jit at SLIC.CELLBIO.DUKE.EDU
Tue Apr 9 15:00:55 AEST 1991


In message <9104082133.AA15472 at karron.med.nyu.edu>

>>I want to pass a LIST of lines, or a LIST of planes, where there can be an
>>arbitrary number of elements. I can do this by blinding the compiler with
>>void * and &p->LineInstance.V[0][0] but I would like the debugger and the
>>comiler to know the array bounds for what is pointed by.

It just occurred to me that I may not reply to some of your message, which
is probably what you want to know.

I usually deference the whole array to a simple one dimension.
Then knowing that C array is a column-major array, 
I can just calculate the position in the array like a matrix.
You may not like the way I do it but this will give you a more 
comfortable feeling about array and pointer.

example,
----------
#include <stdio.h>

#define COL 3
#define ROW 4

main()
{
    int inttype[COL][ROW]; 
    int *odptr; /* one dim. pointer */
    int index, in2;
    
    odptr = inttype[0];

    for (index = 0; index < COL; index ++) 
	for (in2 = 0; in2 < ROW; in2 ++)
	    inttype[index][in2] = in2 + ROW*index;


/* If you reverse the order of ROW and COL, for large arrays, this
   could create a lot of page faults.
    for (in2 = 0; in2 < ROW; in2 ++)	
        for (index = 0; index < COL; index ++)
            inttype[index][in2] = in2 + ROW*index;
*/
    
    /* it is important to declare odptr correctly so that
       the compiler will know how many bytes to advance 
     In other words, array inttype[2][3] would be :  */

    *(odptr + 2*ROW + 3) = 7777;
    
    
    for (index = 0; index < COL; index ++) 
	for (in2 = 0; in2 < ROW; in2 ++)
	    printf("col= %d, row=%d, value=%d\n",
		   index, in2, inttype[index][in2]);
    
    for (index = 0; index < ROW*COL; index ++) 
	printf("index = %d, content=%d\n",index, *odptr++);

/* watch out. odptr points beyond the last element here. */
    
}



More information about the Comp.sys.sgi mailing list