1

I'm solving an algorithm problem using C++.

I want to put the pointer of 2-dimensional array that having different sizes of each dimension by init input dynamically.

What I coded as a function is below: the contents(the function of function) mean nothing.

int cal(int **arr){
int test = arr[0][0];
return 0;
}

And the result of this function

int arrayD[totalGroupCount][totalBeadCount];

int a = cal(arrayD);

It just says "No Matching function call for 'cal'"

I did declare the function 'cal'.

And I did it a lot with different symbol

int cal(int *arr[]){
int test = arr[0][0];
return 0;
}

But it says me identically.

I already searched for this question but answers I got make just the same error(I totally don't understand how they make it)

alk
  • 69,737
  • 10
  • 105
  • 255
LKM
  • 2,410
  • 9
  • 28
  • 53
  • 1
    Note that type of `int arr[0][0]` is actually `int *` and not `int **`. – Dima Tisnek Oct 12 '15 at 16:37
  • Please read up on multidimensional arrays, e.g. here: http://www.cplusplus.com/doc/tutorial/arrays/ – Dima Tisnek Oct 12 '15 at 16:38
  • Also note that `&arrayD` is *not* a `int**`. – alk Oct 12 '15 at 16:39
  • 1
    @qarma: Err, what? The type of `int arr[x][y]` is `int[x][y]` not `int*`. – alk Oct 12 '15 at 16:41
  • You want to use `int cal((arrayD*) [totalBeadCount]);`. – alk Oct 12 '15 at 16:45
  • @qarma int arr[x][y] is not int * – LKM Oct 12 '15 at 16:45
  • @alk Thank you for your answer but totalBeadCount is dynamic integer value... – LKM Oct 12 '15 at 16:47
  • I think it's very difficult to solve more than I imagine – LKM Oct 12 '15 at 16:47
  • Then pass is a first parameter to the function: `int cal(size_t totalBeadCount, (arrayD*) [totalBeadCount]);`. This however is very Cish, C++ provides much more elegant ways to solve this. – alk Oct 12 '15 at 16:49
  • `a = cal(&arrayD[0])` should compile. But this is not recommended anyway. In practice, casting a multidiensinal array to a pointer works only if the dimensions of the array are known except the first one. Since you have C++, use it, use stl! – A.S.H Oct 12 '15 at 16:49
  • @alk details, details, I'm saying what it's logically closer to – Dima Tisnek Oct 12 '15 at 16:51

2 Answers2

2

As you use c++, there's better solution with std::vector< vector<int > >

int cal(std::vector<std::vector<int> > arr)
{
    int test = arr[0][0];
    return 0;
}

And call the function

std::vector<std::vector<int> >arrayD (totalGroupCount, std::vector<int>(totalBeadCount));
int a = cal(arrayD);

Also you can use push_back() function to dynamically add element to the vector.

ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42
0

You need to allocate memory with malloc, calloc, or new:

long a;
int **pt; // a pointer to pointer to int
pt=new int*[rows]; // allocate memory for pointers,
// not for ints

for (a=0;a<rows;++a) pt[a]=new int[columns]; // here you're allocating
// memory for the actual data

This will create an array similar to pt[rows][columns].

Then you pass pt like this:

int Func(int **data) {
//do something
return //something
}

Func(pt);
ForceBru
  • 43,482
  • 10
  • 63
  • 98