Reputation: 37
void PRINT_LCS(int b[][], string x, int i, int j){
if(i==0 || j==0)
cout<<x[0];
if(b[i][j] == 1){
PRINT_LCS(b, x, i-1, j-1);
cout<<x[i];
}
else if(b[i][j] == 2)
PRINT_LCS(b, x, i-1, j-1);
else
PRINT_LCS(b, x, i, j-1);
}
this is my program but i don't know why the first line has an error. The error messages are given below:
error: declaration of 'b' as multidimensional array must have bounds for all dimensions except the first|
error: expected ')' before ',' token|
error: expected initializer before 'x'|
Upvotes: 1
Views: 121
Reputation: 780
When you declare an unidimensional array as int b[]
, the compiler does not know the size of that array, but it use b as a pointer of int and is able to access to its components, being the responsibility of the programmer to be sure that access is in the bound of the array. So, this code:
void f(int b[]) {
b[5] = 15; // access to 5th element and set value 15
}
is equivalent to:
void f(int *b) {
*(b + 5) = 15;
}
If you use a bidimensional array, the rows of the array is stored consecutively in memory, so the compiler needs to know the column size to access to an arbitrary element in the array. (Is still responsibility of the programmer to be sure the access is not out of bound). Now, this code:
void f(int b[][COLUMN_SIZE]) {
b[5][3] = 15; // access to 5th row, 3rd column and set value 15
}
is equivalent to:
void f(int *b) {
*(b + 5 * COLUMN_SIZE + 3) = 15;
}
But if you don't specify the column size, how the compiler know its size? This is generalizable for multidimensional array.
Upvotes: 0
Reputation: 11649
You have to pass the second (column) dimension of the 2D array when declaring an array in function arguments. In your case:
void PRINT_LCS(int b[][COLUMN], string x, int i, int j) //replace column with the no of cols in your 2D array
Upvotes: 4