🔒 Locked
Pointer to multidimensional array
Advertisement
Original Post
What is wrong with the following statement (in my Ctor of a class):
MARKET * mDemand = new MARKET[300][300];
It gives the following error:
error C2440: ''initializing'' : cannot convert from ''struct MARKET (*)[277]'' to ''struct MARKET *''
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
quote:
Original post by Anonymous Poster
MARKET ** mDemand = new MARKET[300][300];
Nope...
"cannot convert from ''struct MARKET (*)[277]'' to ''struct MARKET ** ''
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast"
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
Either:
or:
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
MARKET** mDemand = new MARKET*[300];
for (std::size_t idx = 0; idx < 300; ++idx)
mDemand[idx] = new MARKET[300];
...
for (std::size_t idx = 0; idx < 300; ++idx)
delete [] mDemand[idx];
delete [] mDemand;
or:
std::vector< std::vector<MARKET> > mDemand( 300, std::vector<MARKET>(300) );
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]
MARKET *mDemand = new MARKET[90000];Will work just fine, as will:
MARKET mDemand[300][300]but since the following doesn't work:
MARKET *mDemand = new MARKET[90000];
mDemand[25][30].something()because the computer doesn't know how large either of the dimensions of the array are, why would you expect:
MARKET * mDemand = new MARKET[300][300];to work either?????
"Multi-dimensional arrays must be declared multi-dimensional, although a pointer may be passed into a function which expects a multi-dimensional array" From the "Big C++ Book"
Here's to the crazy ones, the misfits, the rebels, the troublemakers, the round pegs in the square holes, the ones who see things differently.
[edited by - swiftcoder on November 22, 2003 4:45:51 PM]
quote:
Original post by jungle
May I suggest
MARKET (*mDemand)[300]=new MARKET[300][300];
That seems to work. Now that I have this array declared on the heap in my Ctor, all the references to mDemand aren''t seeing it. I assume that I need to do something in the header file that shows that this bloody thing exists so that the functions can see it as well?
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
Another way to tackle this is to avoid multiple dimensionality completely:
Granted, you''re performing a multiplication every time you retrieve an element from the array, but if you use powers of two for your dimension limits, you can perform a lot of optimizations (i.e. bit shifting).
matrix³
[ email | web | apple gl | linux gl | opengl | freeglut | glui | lua ]
int max_x = 300;
int max_y = 300;
int* int_array = new int[x * y];
// retrieve element n
int get_element(int x, int y)
{
return int_array[y * max_x + x];
}
Granted, you''re performing a multiplication every time you retrieve an element from the array, but if you use powers of two for your dimension limits, you can perform a lot of optimizations (i.e. bit shifting).
matrix³
[ email | web | apple gl | linux gl | opengl | freeglut | glui | lua ]
My problem now is that I was trying to trick the damn thing (a la a different thread) into allowing me to do 4 dimensions. The struct that you see there is now giving me fits because it contains an array of yet ANOTHER struct with an array. Can I just use the tricks for this 2x array and extend it out to 4x?
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
quote:
Original post by Anonymous Poster
How do you pass a multidimensional array to a function? And how would I access the array inside of the function?
Are you asking me?
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
void PrintFirst(char ** MultDimArray)
{
cout << "The first string in the string list is: " << MultDimArray[0] << endl;
}
void main()
{
//it's a lot more intuitive to do this using malloc
char** MDA = (char**)malloc(num_strings * string_len * sizeof(char*));
PrintFirst(MDA);
}
Didn't try to compile it - so I can't guarantee it'll work... Substitute 'char' with 'MARKET' to get the desired result.

[edited by - crispy on November 23, 2003 12:55:37 PM]
well... if you need dynamically sized arrays, and need higher dimensions... just use more indirection...
This gives us a 4D array, and similarly, when the time comes to delete the whole thing...
Looking at it, that delete code looks a little strange, but I think it works fine, anyway, I hope you get the general idea.
int ****array=new int***[10];
for (int x=0;x<10;x++) {
array[x]=new int**[10];
for (int y=0;y<10;y++) {
array[x][y]=new int*[10];
for (int z=0;z<10;z++) array[x][y][z]=new int[10];
}
}
This gives us a 4D array, and similarly, when the time comes to delete the whole thing...
for (int x=0;x<10;x++) {
for (int y=0;y<10;y++) {
for (int z=0;z<10;z++) delete[] array[x][y][z];
}
for (int y=0;y<10;y++) delete[] array[x][y];
}
for (int x=0;x<10;x++) {
delete[] array[x];
}
delete[] array;
Looking at it, that delete code looks a little strange, but I think it works fine, anyway, I hope you get the general idea.
boost::multi_array - Multidimensional containers and adaptors for arrays of contiguous data.
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement