passing a multidimensional array to a function?

how would i go about passing a multidimensional array from one function to another?
Are you familiar with pointers? I'd try and return a pointer to the über array.
not really familiar with them, i know the very basics of how they work, but sadly i haven't learned much about them.

mind writing a very small,simple code for how you would do it?
The way you do this depends on what kind of multidimentional array you're dealing with, as each kind is unique and they cannot be interchanged (ie: writing a function that works for one kind will not worth with the other kinds).

Outlines of how to pass each kind to a function are in this thread, along with some reasons why you should try to avoid MD usage and some alternatives:

http://cplusplus.com/forum/articles/17108/
well, i wouldnt use MD arrays without a reason, i use it to create a tile map. i want to pass it to another function to avoid having to declare it inside of a public function. would it be very dumb to just declare it in a public function?
tile maps don't really need to be a 2D array. You can implement them easily with a 1D mimic (See above link).

They probably should be objectified anyway. Ie you should be passing around a TileMap class or something rather than the array directly.
I remember reading about doing this, but never bothered remembering it as I myself hadn't found a need to do such a thing (yet). I'm thinking it says that you only have to specify the second part of the array?

1
2
3
4
 
int array[2][4] = {'\0'};

void function(array[][4]);


Could be wrong as I'm pulling that from reading memory and not actual usage memory.
Last edited on by closed account z6A9GNh0
well it does seem from what i have read, that you personally dont like the MD arrays, not that they are bad programming. i am i no way at all a very good programmer, but i wont change up my code based on one other programmers opinion, at least not before i know that is the only way to do something. i prefer to try t find a way that works for me, hence why i posted this post in hopes of finding a way to pas on a multidimensional array.
If you're using simple pointers arrays, just pass their pointers. If you're not familiar with that, use std vector, and pass by reference to avoid unnecessary copying

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <vector>

using namespace std;

void myFunc(const vector<vector<vector<double> > >& my3DArray)
{
   //do your stuff with your array
}

int main()
{
   vector<vector<vector<double> > > array;
   array.resize(10);
   for(long i = 0; i < array.size(); i++}
   {
      array[i].resize(20);
      for(long j = 0; j < array[i].size(); j++}
      {
         array[i][j].resize(30);
      }
   }
   //now your array's size is 10x20x30. Call your function!!
   
   myFunc(array);
   return 0;
}
Topic archived. No new replies allowed.