0

I'm working on doing calculations in a two dimensional array

but keep getting a nasty error.

i call the function by :

if(checkArray(array))

and try to pass it in like this:

bool checkArray(double array[][10])  //or double *array[][10] to no avail  

the error is

error: cannot convert ‘double ()[(((unsigned int)(((int)n) + -0x00000000000000001)) + 1)]’ to ‘double’ for argument ‘1’ to ‘bool checkArray(double*)’

code snippet

//array declaration

int n = 10;
double array[n][n];

//function call to pass in array


   while(f != 25)
   {

        cout<<endl;
    cout<<endl;

        if(checkArray(array)) //this is the line of the error
         {
      cout<<"EXIT EXIT EXIT"<<endl;
        }

        f++;

    }

    //function declaration

       bool checkArray(double *array)//, double newArray[][10])
       {
            double length = sizeof(array);
            for(int i = 0; i < length; i++)
            for(int j = 0; j < length;j++)
            {
                double temp = array[i][j];
                    }
         }
Lazer
  • 90,700
  • 113
  • 281
  • 364
John Marcus
  • 23
  • 1
  • 1
  • 5
  • 1
    Please give us a code snippet which produces the error so we can help you. – moinudin Dec 18 '10 at 00:39
  • You might want to edit this again, so the code shows up correctly. Make sure each line as at least 4 spaces in front of it, and you can use the preview at the bottom of the page – Sqeaky Dec 18 '10 at 01:02
  • possible duplicate of [How do I pass a reference to a two-dimensional array to a function?](http://stackoverflow.com/questions/404232/how-do-i-pass-a-reference-to-a-two-dimensional-array-to-a-function) – Ben Voigt Dec 18 '10 at 01:25
  • Another dupe: http://stackoverflow.com/questions/4434395/c-passing-dynamically-sized-2d-array-to-function – Ben Voigt Dec 18 '10 at 01:48

2 Answers2

2

When I look at the error you are getting, I have an impression that your function has got invalid declaration. It looks like as if it would expect only one-dimensional array: double*.

However, your question seems a little unclear for me... Could you paste the function code?

Lukasz
  • 7,572
  • 4
  • 41
  • 50
0

Is this really a direct (if edited) copy of your code?

This line:

int n = 10; double array[n][n];

is not valid C++. You can't declare an array with variable dimensions. This would work:

const int n = 10; double array[n][n];

You want to declare checkArray as:

 bool checkArray(double array[][10])

and you absolutely do not want to do this:

double length = sizeof(array);

because that will assign to length the size of a pointer, in bytes (4 or 8.) You need to pass in the number of rows explicitly. Also, you're much better off declaring length as an int, or better yet a size_t.

This seems like a decent resource: http://www.fredosaurus.com/notes-cpp/arrayptr/22twodim.html

Dan Breslau
  • 11,472
  • 2
  • 35
  • 44
  • Thanks so much the const was what was needed. I'm not very good with c++ because I have a strong java background. I declared the n because I want to have input from the user to set the dimensions of the array – John Marcus Dec 18 '10 at 01:26
  • Glad to have helped. But if `n` is `const`, then you can't change its value through user input, and you can't use a non-const variable to declare an array's dimensions. If you need to size the array dimensions at runtime, you need to do something like this: `double **array = new (*double)[n]; for (int i = 0; i < n; i++) {array[i] = new double[n];}` Then, when you're done with the array, you need to delete it in the opposite order: `for (int i = 0; i < n; i++) {delete [] array[i]}; delete [] array;` – Dan Breslau Dec 18 '10 at 01:30
  • @John Marcus: Also, if the answer did help, please uprate it (click on the up arrow to the left.) If it was the best answer, please "accept" it. It's in your best interest to reward those who've helped you. – Dan Breslau Dec 18 '10 at 01:35