PDA

View Full Version : Pointer math: incrementing through a 2d array


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?

jamessan
07-12-2003, 11:53 AM
You almost have it correct.int const ROWS = 10;
int const COLS = 10;

int myArray[ROWS][COLS];
int *ptrInt;
int intOffset;

ptrInt = &myArray[0][0];

for (intOffset = 0; intOffset < ROWS * COLS; intOffset++)
{
*(ptrInt + intOffset) = intOffset;
}The only thing that you had off was that ptrInt needs to be assigned the address at the beginning of myArray. 'ptrInt = myArray;' attempts to assign an int to an int*. It probably isn't a good idea to use pointers like this in multidimensional arrays because I don't think you can assure that everything is contiguous.

DNAunion2000
07-12-2003, 01:32 PM
jamessan: You almost have it correct.


int const ROWS = 10;
int const COLS = 10;

int myArray[ROWS][COLS];
int *ptrInt;
int intOffset;

ptrInt = &myArray[0][0];

for (intOffset = 0; intOffset < ROWS * COLS; intOffset++)
{
*(ptrInt + intOffset) = intOffset;
}

The only thing that you had off was that ptrInt needs to be assigned the address at the beginning of myArray. 'ptrInt = myArray;' attempts to assign an int to an int*.

DNAunion2000: Okay, now I'm a bit confused.

The name of an array, without subscripts, returns a pointer to the first element of that array. So both &myArray[0][0] and myArray should yield the same thing: a pointer to the first integer stored in the array myArray. Right? Apparently not. I tried compiling it both ways and yours produced a clean compile but mine didn't.

So does that rule about unsubscripted array names apply only to one-dimensional arrays?


************************
Edited to add a quick quote.



char str[80];
char *p1;

p1 = str;


Here, str is an array of 80 characters and p1 is a character pointer. However, it is the third line that is of interest. In this line, p1 is assigned the address of the first element in the str array. (That is, after tha assignment, p1 will point to str[0].) Here's why: In C++, using the name of an array without an index generates a pointer to the first element in the array. Thus, the assignment


p1 = str;


assigns the address of str[0] to p1. This is a crucial point to understand: When an unindexed array name is used in an expression, it yields a pointer to the first element in the array." (Herbert Schildt, C++: A Beginner's Guide, McGraw-Hill, 2002, p162-163)