# Convert a two-dimensional array to a one demensional array

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-10-2008
belkins
Convert a two-dimensional array to a one demensional array
Well the title pretty much explains itself...anyone know how to do this?
• 11-10-2008
zacs7
Yes. Pointer arithmetic and/or cast :p
• 11-10-2008
belkins
so if i had an array for example power[8][7], i would cast it how?
• 11-10-2008
itCbitC
power1D[56] == power2D[8][7]
• 11-10-2008
tabstop
Of course you can't assign arrays-as-a-whole. But you could do int *power1D=power2D[8][7], and pretend that power1D has 56 elements.
• 11-10-2008
belkins
so it must have that pointer
• 11-10-2008
tabstop
You could use a real array if you want, instead of a pointer. But you'd have to use a big ol' for-loop to get all the numbers in the new array.
• 11-10-2008
belkins
ok ill go with the pointer
• 11-10-2008
itCbitC
As long as you access the 2D array elements in their proper storage order you can use either a pointer with an offset or a subscripted array.
The thing to remember is to walk along the 2D array correctly knowing that the rightmost subscript varies fastest.
• 11-10-2008
belkins
so does this work NROWS being 8 and NCOLS being 7
Code:

```int i, j, k; int power1d[56]; for(i = 0; i<NROWS; i++) { for(j=0; j<NCOLS; j++) { for(k=0; k <56; k++) power1d = power[i][j]; }}```
• 11-10-2008
tabstop
Quote:

Originally Posted by belkins
so does this work NROWS being 8 and NCOLS being 7
Code:

```int i, j, k; int power1d[56]; for(i = 0; i<NROWS; i++) { for(j=0; j<NCOLS; j++) { for(k=0; k <56; k++) power1d = power[i][j]; }}```

No. You can never assign to the name of an array. Fortunately, you don't want to assign the name of an array, but to a particular slot in the array.
• 11-10-2008
belkins
does another array need to be declared?
• 11-10-2008
tabstop
Quote:

Originally Posted by belkins
does another array need to be declared?

Of course not.
• 11-10-2008
belkins
do i declare the 2-d array as one-d then
• 11-10-2008
tabstop
Quote:

Originally Posted by belkins
do i declare the 2-d array as one-d then

Well, wait. I thought you meant another another array. The one one-d array you already have is fine. But you have to assign to array[something], not just array.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last