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]'

shinzou
  • 5,850
  • 10
  • 60
  • 124
  • 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 – nbro Dec 20 '14 at 22:25
  • @nbro I don't understand the question but `cout << a(arg[][SIZE], 3, 3);` doesn't work either... – shinzou Dec 20 '14 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 – nbro Dec 20 '14 at 22:29
  • @nbro How is this different from a single dimension array? btw I just learned arrays so I'm a total newbie. – shinzou Dec 20 '14 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... – nbro Dec 20 '14 at 22:35
  • "you have to pass a multidimensional array that respects the sizes required by the function signature" So is the only way to do this for the two dimensions is with a template like in Berry's answer? @nbro – shinzou Dec 20 '14 at 22:43
  • You can simply do as his first example, what is the problem? – nbro Dec 20 '14 at 22:44

1 Answers1

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.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • What's with the downvotes here? I'll have to look up that syntax `double**` and decayed... – shinzou Dec 20 '14 at 22:26
  • @kuhaku I suggest picking up a good C++ book. It's far too complicated a topic to explain in a comment. – Barry Dec 20 '14 at 22:36
  • Is there a way to pass both dimensions without using a template? – shinzou Dec 20 '14 at 22:44
  • @kuhaku The only other way is kind of a hack, see [this answer](http://stackoverflow.com/a/5329380/2069064), part 3. – Barry Dec 20 '14 at 22:46
  • 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 or Queue Stack stack. It is a "generic" container – David Coler Dec 21 '14 at 05:09
  • It's not. Think of all those things as Containers for information, it doesn't matter what info it is, it only matters how it is stored and retrieved. – David Coler Dec 21 '14 at 18:35
  • That is an absolutely horrible description of templates. – Barry Dec 21 '14 at 22:04
  • its a very simplistic description of templates – David Coler Dec 25 '14 at 21:23