954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

multidimensional array

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 >>

allomeen
Newbie Poster
9 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

I find the diagnostic message quite self-explanitory.declaration of `c' as multidimensional array must have bounds for all dimensions except the firstYou can't do[][]. You can do [][5], for example.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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.

Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You