-3

When I create a multidimensional array like value[][],and pass it to the function.But I am quite confused about how to write a prototype for this function,because there is an error message like:

   error: declaration of 'value' as multidimensional array must have bounds for all dimensions except the first|


   //function prototype;
  void matrix(double s[],int j,int n,double value[][],double alpha[], double beta[], double gamma[]);
  //function
   void matrix(double s[],int j,int n,double value[][],double alpha[], double beta[],double gamma[]){...}

help,How to write it correctly?

user3570984
  • 33
  • 1
  • 6
  • 1
    possible duplicate of [passing 2D array to function](http://stackoverflow.com/questions/8767166/passing-2d-array-to-function), [also check this](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c/4810668#4810668) – Koushik Shetty May 04 '14 at 05:39
  • The error is fairly obvious. You have to provide values for all dimensions except the first one in a method signature. – Rakib May 04 '14 at 05:41
  • ty,but wut if i don't know the exact number for the second dimension?say,int k;int j; double value[k][j]; and i need to pass this two dimensional array to an function and j is given by expert which i dunno in advance. – user3570984 May 04 '14 at 06:05

3 Answers3

1

the compiler already told you error: declaration of 'value' as multidimensional array must have bounds for all dimensions except the first|

you need specify the length, eg: double s[3], double[3][3] etc.

tim
  • 121
  • 4
1

The error message from the compiler is very telling. The argument double value[][] needs to be changed to double vale[][N] where N is a integer. Examples:

double value[][20] // A matrix in which each row has 20 columns.
double value[][10] // A matrix in which each row has 10 columns.
R Sahu
  • 196,807
  • 13
  • 136
  • 247
  • ty,but wut if i don't know the exact number for the second dimension?say,int k;int j; double value[k][j]; and i need to pass this two dimensional array to an function and j is a variable which could be given by expert and i dunno in advance. – user3570984 May 04 '14 at 06:08
  • @user3570984 if the user is passing in the values like that, you might as well just use `double **value` instead – RamblingMad May 04 '14 at 06:38
0

The folks above have given the technical explanation.

The reason behind is is that C and C++ treat arrays as blocks of memory. When an array gets passed to a function, all that gets sent is a pointer to the array. This is in stark contrast to Ada, Pascal, and Fortran that send descriptors of the array to the function.

Your declaration must provide enough information to work with the array with just a pointer passed. That means is need all but the last (first specified) array dimension.

Multidimensional arrays of variable size are usually a sign of bad design. If you have a fixed array size (4x4 is common in 3D transforms), 2D array work out well. For something like general purpose matrix operations, 2D arrays do not work well.

For that you need to define a class that: 1) Manages a 1D array 2) Provides the mechanism for translating 2D references into 1D index.

What you would do is something like

class Matrix 
{
  unsigned int range1 ;
  unsigned int range2
  double *values ; // Could use a template

  double &valueAt (unsigned int x, unsigned int y) { return values [range1 * x + y] ; }
} ;
user3344003
  • 18,590
  • 3
  • 22
  • 52
  • ty,but wut if i don't know the exact number for the second dimension?say,int k;int j; double value[k][j]; and i need to pass this two dimensional array to an function and j is a variable which could be given by expert and i dunno in advance.So,in this case, how can i write this function array parameter? – user3570984 May 04 '14 at 06:10
  • You can't really do what you're asking for direct. – user3344003 May 04 '14 at 06:12
  • can i write a function template to indicate an arbitrary number for the second parameter ? – user3570984 May 04 '14 at 06:20
  • Yes sir.I got the solution as follows,and that works! template void compare(string name, double value[][N]){ } – user3570984 May 04 '14 at 06:24