Re: convert 1D array to 3D array
andy napsal:
Dear Readers:
I tried to convert 1D array to 3D array but failed. Is there any
possible way to do that?
If not, what is the simpler way to map 1-dimension array to
multi-dimension array.
Thanks in advanced.
int main(void)
{
int m1[3*4*5];
int m3[3][4][5];
m1[1*4*5 + 2*5 +3] = m3[1][2][3] = 10;
int (*m2)[4][5] = (int *[4][5])m1; ======> line 18
printf(" m1[1][2][3] = %d\n", m2[1][2][3]);
}
multiarray.cpp:18: error: ISO C++ forbids casting to an array type
`int*[4][5]'
Cheers,
Andy
Hi. See function 'Set' for conversion:
#include <iostream>
#include <iomanip>
static const size_t A = 5;
static const size_t B = 4;
static const size_t C = 3;
static int m1[A * B * C];
static int m3[A][B][C];
void Set(size_t x, size_t y, size_t z, int value)
{
m1[x + A * (y + B * z)] = m3[x][y][z] = value;
}
int main(void)
{
int value = 0;
// Set sequentialy all values
for (size_t k = 0; k < C; ++k)
for (size_t j = 0; j < B; ++j)
for (size_t i = 0; i < A; ++i)
Set(i, j, k, ++value);
// Dump 3d array
for (size_t k = 0; k < C; ++k)
{
for (size_t j = 0; j < B; ++j)
{
for (size_t i = 0; i < A; ++i)
{
std::cout << std::setw(5) << m3[i][j][k];
}
std::cout << "\n";
}
std::cout << "\n";
}
// Dump 1d array
for (int i = 0; i < A * B * C; ++i)
{
std::cout << std::setw(5) << m1[i];
}
std::cout << "\n\n";
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]