1

I have array with known x size (5) and the y is taken from variable, so the array is something like this - array[5][y];

And now I'm quite troubled how to pass it to function, I won't edit it, just read the data from it.

I thought to do something like this:

void someFunction(double **array)

but I get

convert error: cannot convert `double (*)[((unsigned int)((int)n))]' to `double**' for argument `2' to `void findMax(int, double**, int)'|
2
  • Please post the code you are having trouble with. Post the function definition and your calling code. Commented Mar 18, 2013 at 13:18
  • Use std::vector or std::array instead. Commented Mar 18, 2013 at 13:18

2 Answers 2

1

For Array [5][x]

Unfortunately you cannot define a type for an array like double[5][]. You can only omit the first dimension of a multidimensional array, not the last.

For Array [x][5]

You should go with void someFunction(double array[][5], int size). And then you loop trough the elements with for (int i = 0; i < size; i++).

Example:

void someFunction(double array[][5], int size) {
    for (int k = 0; i < size; i++)
        for (int i = 0; i < 5; i++)
            std::cout << array[k][i] << std::endl;
}


double a[10][5];
// populate data
someFunction(a, 10);

Usually it's preferred to use std:: containers instead of raw C arrays.

Take a look at std::vector for example.

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

4 Comments

I know about vectors, but the thing is that teachers wants me to do some excercises with arrays, because according to her std is for "lazy people" ^^
@EdgarasAusvicas, I've been there aswell. I know what you are talking about.
by the way now im getting this: declaration of `array' as multidimensional array must have bounds for all dimensions except the first
@EdgarasAusvicas your teacher should not be allowed to teach C++.
0

If you are not using the STL, you can also do this:

void someFunction(double **mat, const int Xsize, const int Ysize )
{
    for(unsigned int i = 0 ; i < Xsize ; ++i)
    {
        for(unsigned int j = 0 ; j < Ysize ; ++j)
        {
            std::cout << ((double *)mat + Ysize * i)[j] << "  " ;
        }
        std::cout << std::endl;
    }

    return ;
}

And call the function like this:

int main(int argc, char** argv)
{
    double matrice[2][3] = {{1,2,3},{4,5,6}};
    someFunction( (double**)matrice, 2, 3 );
    return 1;
}

Comments

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.