Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pointer to multidimensional array

Started by IADaveMark Nov 22, 2003 at 3:11 PM 21 replies 4.3k views
Original Post
IADaveMark
IADaveMark
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!"
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit<
IADaveMark
IADaveMark
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!"
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit<
jungle
jungle
May I suggest
MARKET (*mDemand)[300]=new MARKET[300][300];

Lektrix
Lektrix
Either:

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 ]
swiftcoder
swiftcoder
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]
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
IADaveMark
IADaveMark
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!"
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit<
MatrixCubed
MatrixCubed
Another way to tackle this is to avoid multiple dimensionality completely:



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 ]
IADaveMark
IADaveMark
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 of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit<
IADaveMark
IADaveMark
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!"
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit<
Crispy
Crispy

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]
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
dmounty
dmounty
well... if you need dynamically sized arrays, and need higher dimensions... just use more indirection...


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.
jungle
jungle

void foo_func(int (*p)[30][40][50]) {
// zero one element

p[5][19][12][1]=0;
}

int main()
{
// allocate 4D array, 100 x 30 x 40 x 50

int (*arr)[30][40][50]=new int[100][30][40][50];

// pass to function

foo_func(arr);

// delete array

delete [] arr;
}
jungle
jungle

void PrintMulti(int (*a)[5]) {
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; j++)
cout << a[i][j] << endl;
}
}

int main() {
int a[5][5];
PrintMulti(a);

system("PAUSE");
return 0;
}
Fruny
Fruny
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Oluseyi
Oluseyi
Why necro a dead thread?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.