Hi, I having this error and i don't know how to fix it, can anybody help please:
"declaration of `c' as multidimensional array must have bounds for all dimensions except the first", i'm getting this error because of :

``````void lcs::LCS_Length(int X[], int Y[], int c[][], char b[][],  int m, int n)
{
for( int i = 1; i < m; i++)
c[i][0] = 0;
for( int j = 1; j < n; j++)
c[0][j] = 0;
for( int i = 1; i < m; i++)
for( int j = 1; j < n; j++)
{
if( X[i] == Y[j])
{
c[i][j] = c[i - 1][j - 1] + 1;
b[i][j] = 'D';
}

else if(c[i - 1][j] >= c[i][j - 1])
{
c[i][j] = c[i - 1][j];
b[i][j] = 'U';
}
else
{
c[i][j] = c[i][j - 1];
b[i][j] = 'L';
}
}
}``````

<< moderator edit: added [code][/code] tags >>

royali commented: hi,i have a program c[i][j]a[i][j]*b[i][j] in home task.but i can not do this program.please anyone can solve my program? +0

All 8 Replies

I find the diagnostic message quite self-explanitory.

declaration of `c' as multidimensional array must have bounds for all dimensions except the first

You can't do [][]. You can do [][5], for example.

You can omit the array size for the first dimension because array names are almost always converted to a pointer to the first element, so any size information is lost. That feature only applies to the first dimension, so you have to provide size information for the second dimension because the conversion to a pointer makes it a pointer to an array of N. These two function declarations are equivalent:

``````void foo(int a[][10]);
void bar(int (*a)[10]);``````

To avoid those errors, you can always provide sizes for all dimensions until you're comfortable with the rules. However, for arbitrary sizes in all dimensions, you have no choice but to use a dynamic array:

``````void foo(int **a, int m, int n);

int main()
{
int **a = new int*[5];

for (int i = 0; i < 5; i++)
a[i] = new int[5];

foo(a, 5, 5);

for (int i = 0; i < 5; i++)
delete [] a[i];
delete [] a;
}``````

Of course, that's assuming you don't have libraries to work with. The standard vector class is well suited to this:

``````#include <vector>

void foo(std::vector<std::vector<int> > a);``````

The boost library also supports a multi_array class.

commented: nice! +0

hey, There is a very huge confusion in my mind for the representation of arrays.

``````int a[10][10];
``````

is same as,

``````int(*a)[10];
``````

which means a pointer to an array of 10 intergers. right ?

If yes, then how these statements are correct ?

``````*(*(a+1)+1)
``````

or,

``````*(*(a+2)+1)
``````

these statements give correct answers. But, Here it is 2 times dereferecing operator is used which means a is not a pointer to an array of 10, rather it is a pointer to a pointer. I hope i am making my question very clear. please help in this.

int a[10][10];

is same as,

int(*a)[10];

Err, NO.

The first, a is an array of 10 arrays of 10 integers.

The second a is a pointer to an array of 10 integers.

Assuming the first, `int a[10][10];`, then the expression `a` evaulates to a type `int(*)[10];`.

Which is the same type as a in the second case.

So then break down the statment `((a+1)+1)'

a is a pointer to an array of 10 ints
a+1 increments a, the compiler knows the size of a so it increments it by that size to the next array of 10 ints in the array
(a+1) converts us from a pointer to an array of 10 ints to an array of 10 ints which evaluates to a type `int *`
*(a+1)+1 increments our int * point to the next one in the array
*(
(a+1)+1) converts out int * to an int

Basically giving the second integer in the second array of 10 ints, functionally equivilent to `a[1][1]`.

commented: quite explaintary. +2

hey,
You make me clear at one step amd again confused me in another step.

a+1 means pointer to next array of 10 integers. it is ok.
then *(a+1) means the value of (a+1), which is the array of 10 integers. it is ok.
So, now we have array in our hands,
then just doing *(a+1)+1 will give the second element of array. So, you got my confusion i think.

Making myself more clear,

if

``````*(a+1)+1
``````

give us the second element, then why we again derefercning it using "*" ? i hope i am clear in my question.

This may help. It's written by Chris Torek, who's a man deserving of great respect due to his deep knowledge of C.

@decptikon he is really a great person I think. I never heard of him, But If he is a man for having great respect, then you are also not less than that.