0

I'm trying to use multidimensional arrays but I get an error and no matter what variation I try and I can't seem to be able to get it to work, I couldn't find any help in the documentation online either...

const int SIZE = 50;
double a(double A[][SIZE], int m, int n);

void main(){
    double arg[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    cout << a(arg[SIZE], 3, 3);//I get an error here
}

Why doesn't cout << a(arg, 3, 3); work either? arg in the function is pointer so why is it even needed to specify size or whatever?

The error I get is:

Error 1 error C2664: 'double MeanMatrix(double [][50],int,int)' : cannot convert argument 1 from 'double' to 'double [][50]'

7
  • 1
    Shouldn't you pass an array with the specified size as parameter? Why are you passing an array 3x3, if it's required 1 AxSIZE, where A is not required to be known Commented Dec 20, 2014 at 22:25
  • @nbro I don't understand the question but cout << a(arg[][SIZE], 3, 3); doesn't work either... Commented Dec 20, 2014 at 22:28
  • How can you not understand the question? You function requires a bidimensional array, with size of the second dimension as SIZE, which is equals to 50 Commented Dec 20, 2014 at 22:29
  • @nbro How is this different from a single dimension array? btw I just learned arrays so I'm a total newbie. Commented Dec 20, 2014 at 22:32
  • With multidimensional arrays, to be declared as function parameters, the sizes of their dimensions must be known at compiled time, except for the first dimension, which is exactly what you did. Notice, on the other hand, that you have to pass a multidimensional array that respects the sizes required by the function signature... Commented Dec 20, 2014 at 22:35

1 Answer 1

3

You can pass it in with one dimension stipulated by the function:

double foo(double A[][3], int m);

foo(arg, 3);

Or you can both dimensions deduced by template:

template <size_t M, size_t N>
double foo(double (&A)[M][N]);

foo(arg);

But your function a right now is requiring one of the dimensions to be SIZE, which arg does not satisfy, and regardless you're passing in arg[SIZE], which is undefined behavior since you're indexing past the end of the array and additionally is a double[3], which wouldn't match the type.

Sign up to request clarification or add additional context in comments.

8 Comments

What's with the downvotes here? I'll have to look up that syntax double** and decayed...
@kuhaku I suggest picking up a good C++ book. It's far too complicated a topic to explain in a comment.
Is there a way to pass both dimensions without using a template?
@kuhaku The only other way is kind of a hack, see this answer, part 3.
Simple answer about a template, it is a way to make a class think of stack or queue making your own, and then allowing it to use any data type even that of another class. IE a Stack<int> or Queue<int> Stack<string> stack<put class here>. It is a "generic" container
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.