DNAunion2000
07-12-2003, 12:11 AM
DNAunion2000: I seem to remember that the following is possible:
int const ROWS = 10;
int const COLS = 10;
int myArray[ROWS][COLS];
int *ptrInt;
int intOffset;
ptrInt = myArray;
for (intOffset = 0; intOffset < ROWS * COLS; intOffset++)
{
*(ptrInt + intOffset) = intOffset;
}
DNAunion2000: But I can't get it to compile (of course the above is not the entire program).
Someone told me that the elements of a multi-dimensional array cannot be assumed to be stored in contiguously in memory (compilers are allowed to pad the rows in order to make the align on particular byte boundaries). But the above code, which uses pointer arithmetic to walk across the row boundaries (from myArray[3][9] to myArray[4][0], for example), relies upon all elements being contiguous. Could that be the reason the compiler won't allow the above?
int const ROWS = 10;
int const COLS = 10;
int myArray[ROWS][COLS];
int *ptrInt;
int intOffset;
ptrInt = myArray;
for (intOffset = 0; intOffset < ROWS * COLS; intOffset++)
{
*(ptrInt + intOffset) = intOffset;
}
DNAunion2000: But I can't get it to compile (of course the above is not the entire program).
Someone told me that the elements of a multi-dimensional array cannot be assumed to be stored in contiguously in memory (compilers are allowed to pad the rows in order to make the align on particular byte boundaries). But the above code, which uses pointer arithmetic to walk across the row boundaries (from myArray[3][9] to myArray[4][0], for example), relies upon all elements being contiguous. Could that be the reason the compiler won't allow the above?