pass multi dimensional array to a function

I am trying to pass a multi dimensional array to a function ( by reference ), but I can't seem to figure it out.

I think the issue is with my prototype and function parameters

here is my pseudo code:

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
28
29
30
31
32
33
34
35
36
37
38
39
//  MyClass.h

#define HEIGHT 5
#define WIDTH 5

class MyClass
{
public:

  void giveArray();

private:

   char myArray[HEIGHT][WIDTH];

   void takeArray( char &, int, int);

};


//  myClass.cpp

#include "MyClass.h"

MyClass::MyClass()
{
}

void MyClass::giveArray()
{
   takeArray( myArray[][], HEIGHT, WIDTH )

}

void MyClass::takeArray( char& theArray[][], int h, int w )
{
   //do stuff with the array

}

If it is a class member, you don't need to pass it between class functions...
Anyway, you can have only one implicit dimension in array parameters, when you have more use templates
If it is a class member, you don't need to pass it between class functions...

You are right, I thought I would need to do it this way because I need giveArray() to delegate different arrays to takeArray() but after thinking about it would make more sense in my situation to pass an integer to takeArray() and run a switch on that integer.

anyways, I still would like to know how to pass a multidimensional array to a function say if myArray wasn't a class member and I'm not sure I understand your response?
Bazzy is right. But for reference the syntax is:

1
2
3
4
void giveArray(char (&myArray)[HEIGHT][WIDTH])
{

}

Last edited on
Obligatory link:

http://cplusplus.com/forum/articles/17108/

To actually answer your question... if you want the width and height to be dynamic as you suggest, you'll either have cast to a 1D array, or use a template per Bazzy's suggestion:

1
2
3
4
5
6
7
8
9
10
11
12
// to take a 1D array
void MyClass::takeArray(char* theArray, int h, int w)
{
  // theArray[1][2] = 5;  // no good
  theArray[ (1*w) + 2 ] = 5;  // do this instead
}

// to call it:
takeArray(&myArray[0][0],HEIGHT,WIDTH);
  // but note this only works if the array is declared "straight".  If this is
  //  dynamically allocated (like with nested new), you cannot use the same function
  //  as above.  See previously linked article for more details. 


Or the template route:

1
2
3
4
5
6
7
8
9
10
template <int HEIGHT,int WIDTH>
void MyClass::takeArray( char theArray[HEIGHT][WIDTH] )
{
  // do stuff with array
  // can use 'HEIGHT' and 'WIDTH' as the dims
}

// to call it:
takeArray(myArray);
  // but agian note this only works with straight arrays and not nested new arrays 
I think its called the "right-left" rule for determining the type of variables.

From the variable name, first you look to the right and then you look to the left.

So

 
int i;


The lable is "I" , look to the right? = nothing, look to the left? = int

So i is an int.

 
int a[2];


The lable is "a" , look to the right? = array, look to the left? = int

So a is an array of int

 
int (&a)[2];


Now if we want to make 'a' a reference then we need to override the initial right check by using parentheses.

So the lable is "a" , look to the right? = nothing, look to the left? = reference(&), look to the right? = array, look to the left? = int

So 'a' is a reference to an array of int.

You start at the variable name and work your way from right to left outwards like an onion.

Hope that makes sense!


closed account (1yR4jE8b)
Can't you pass an array of any amount of dimensions through a single pointer, and just do row/column arithmetic on the pointer? It's quite easy with 2/3 dimensional array.

I always do multiple dimension arrays as single dimension arrays, so I'm not quite sure of how actual multi-demensional arrays are laid out in memory though.
@darkestfright:

Yes. My post had an example of that actually. Although it only works if the array is declared "straight", and not "nested new".

And yeah, I avoid MD arrays as well. Pain the butt, they are. About the only time I use them is for static const lookup tables -- and those typically don't get passed around to functions and things.
Topic archived. No new replies allowed.