454,445 Members | 1,329 Online Need help? Post your question and get tips & solutions from a community of 454,445 IT Pros & Developers. It's quick & easy.

# Two-dimensional array to array of arrays

 P: n/a Given the following current definition: int array; Is the following equivalent? (By "equivalent", I mean that any reference to "array[x][y]" retains the same meaning, and that the memory layout of the 200 ints is identical.) typedef int TwoInts; TwoInts array; What about: TwoInts *array = malloc( 100 * sizeof(*array) ); Yes, I realize that type actual type of "array" has changed, and that malloc() may fail, and so on. I just need to verify that "array[x][y]" hasn't changed meaning. A quick test program seems to confirm that the behavior has not changed, but I know that implementation-defined behavior may "work" on one platform an not another. I'm pretty sure that the behavior here is defined by the standard, and not implementation-defined, but I'd like confirmation. Thanks. ========== #include typedef int TwoInts; int array; int main(int argc,char *argv[]) { int i,j; TwoInts *x; for ( i=0 ; i < 10 ; i++ ) for ( j=0 ; j < 2 ; j++ ) array[i][j] = i*10 + j; for ( i=0 ; i < 10 ; i++ ) { for ( j=0 ; j < 2 ; j++ ) printf("[%d][%d] = %2d ",i,j,array[i][j]); printf("\n"); } printf("\n"); x = array; for ( i=0 ; i < 10 ; i++, x++ ) { for ( j=0 ; j < 2 ; j++ ) printf("[%d][%d] = %2d ",i,j,(*x)[j]); printf("\n"); } return(0); } ========== -- +-------------------------+--------------------+-----------------------+ | Kenneth J. Brody | www.hvcomputer.com | #include | | kenbrody/at\spamcop.net | www.fptech.com | Dec 21 '06 #1 