Gonenc
Gonenc

Reputation: 197

Multi-dimensional array

I need to create a function that has a parameter which is a multi-dimensional array with two dimensions being user-specified, e.g.

int function(int a, int b, int array[a][b])
{
 ...
}

How would I do that in C++ ?

Upvotes: 0

Views: 541

Answers (3)

Tamer Shlash
Tamer Shlash

Reputation: 9523

I'm not sure if this work, because your question and code are not the same, according to your code the function can have 3 parameters, so this would work:

int function(int a, int b, int** &array)
{
    array = new int*[a];
    for (int i =0;i<a;i++)
        array[i] = new int[b];

    // I don't know why you are returning int, probably doing something here....
}

However your question says that your function can take only one parameter, so:

  1. if the dimensions are known at compile time, then Fred's Answer is the best (it charmed me in fact! :) ).
  2. if not, I can't see any possible solution that allows passing more than one user-specified value other than encapsulating all these values in one object.

Like this:

class Foo {
public:
    Foo(int d1, int d2)
    { a = d1; b = d2; }
    int a,b;
    int** array;
};

int function(Foo &f)
{
    f.array = new int*[f.a];
    for (int i = 0;i<f.a;i++)
        f.array[i] = new int[f.b];
    // I don't know why you are returning int, probably doing something here....
}

Though I find it a bad idea, in fact the function could be a parameterless method instead:

class Foo {
public:
    Foo(int d1, int d2)
    { a = d1; b = d2; }

    void Create()   // Or could do this right in the Constructor
    {
        array = new int*[a];
        for (int i = 0;i<a;i++)
            array[i] = new int[b];
    }

private:
    int a,b;
    int** array;

};

Still this is a bad idea, because you are reinventing the wheel, as there are a perfect class in the STL to do all the work for you:

vector< vector<int> > v;    // Now v is a 2D array

Upvotes: 0

Mooing Duck
Mooing Duck

Reputation: 66981

Assuming the dimensions are not known at compile time, you emulate a two dimensional array with a one dimensional array:

int& getat(int x, int y, int r, int c, int *array) {return array[y*c+x];}
int function(int a, int b, int *array) {
    getat(4, 2, a, b, array) = 32; //array[4,2] = 32
}

or, for safety, wrap it all in a class:

template <class T>
class array2d {
    std::vector<T> data;
    unsigned cols, rows;
public:
    array2d() : data(), cols(0), rows(0) {}
    array2d(unsigned c, unsigned r) : data(c*r), cols(c), rows(r) {}
    T& operator()(unsigned c, unsigned r) {
        assert(c<cols&&r<rows); 
        return data[r*cols+c];
    }
};

or, best yet, use Boost's Multidimensional Array, which will be better than anything mere mortals could write.

Upvotes: 1

fredoverflow
fredoverflow

Reputation: 263360

Are the dimensions known at compile-time? In that case, turn them into template parameters and pass the array by reference:

template<int a, int b>
int function(int(&array)[a][b])
{
    ...
}

Example client code:

int x[3][7];
function(x);

int y[6][2];
function(y);

Upvotes: 2

Related Questions