2
int LCS_length(char* x, char* y)
{
    int m = strlen(x);
    int n = strlen(y);

    char b[m + 1][n + 1];
    char c[m + 1][n + 1];
}

In this code i want to declare a new two dimensional arrays, but my compiler write me this error:

Expression must have a constant value

Who knows what can i do, because a compiler don't let me do this statement in C language?

NOTE: it just part of the code and it has return statement later.

Hasbridge
  • 173
  • 1
  • 5
  • 14

3 Answers3

5

You must compile the code with a standard C compiler, such as gcc or clang etc. You can't compile using C++ compilers or non-C-compilers such as Microsoft VS.

Other than that, the code is fine apart from missing a return statement.

If you are stuck with old crap compilers, you can alternatively do an old style "mangled array":

size_t m = strlen(something) + 1;
size_t n = strlen(something) + 1;
...
char* b = malloc(m*n);
...
b[i*n + j] = something;
...
free(b);

That is how we used to allocate 2D arrays dynamically back in the old days.

Equivalent code using dynamic allocation in modern standard C would be:

char (*b)[n] = malloc( sizeof(char[m][n]));
...
b[i][j] = something;
...
free(b);
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • I wrote here only part of the code that did not work – Hasbridge May 20 '20 at 13:02
  • i have Return statement later – Hasbridge May 20 '20 at 13:03
  • is the equivalent code using dynamic allocation in modern standard C faster? – Hasbridge May 20 '20 at 13:07
  • @maylor No, any decent compiler will give the very same machine code for both snippets. See the machine code from gcc for example: https://godbolt.org/z/q70JYc. Some `char**` version will be significantly slower though. – Lundin May 20 '20 at 13:15
  • How do you get from (m+1)(n+1) elements in `char b[m + 1][n + 1];` to `sizeof(char[m][n]) + 1`? – Eric Postpischil May 20 '20 at 13:32
  • @EricPostpischil I was simplifying things a bit because `b[i*(n+1) + j]` is some severely ugly code. Hmm hang on, there's a better way yet. Edited. – Lundin May 20 '20 at 13:34
  • The original code seeks to allocate (m+1)(n+1) elements. (The question does not show what it intends to do with them. Perhaps it is a mistake. But it is what it is.) Allocating space for `sizeof(char [m][n])` does not make space for the same number of elements. If the code truly is doing something with (m+1)(n+1) elements, then this allocation is insufficient. (The name, `LCS_length`, suggests [longest common subsequence](https://en.wikipedia.org/wiki/Longest_common_subsequence_problem) but still does not clue us in about their code.) – Eric Postpischil May 20 '20 at 13:54
0

It looks that it wants a specific constant value like: char b[5][5];

edgj4718
  • 47
  • 9
0

Expression must have a constant value

C supports variable sized arrays from C99 standard. Maybe your compiler does not support Variable Length Arrays.

You can see more information at What's the point of VLA anyway?.

And may be helpful: Why aren't variable-length arrays part of the C++ standard?

You can use dynamically allocation instead (Unless you want each index to be a variable-length string, there is really no need for the char** trick: Correctly allocating multi-dimensional arrays):

char **b = malloc((m+1)*sizeof(char *));
if(b) {
   for (int i = 0; i < m+1; i++0 {
      b[i] = malloc(n+1);
      if(!b[i]) {
         // handle the error.
      }
   }
} 

Or you can define the constant value MAX_ROW_LENGTH and MAX_COL_LENGTH:

#define MAX_ROW_LENGTH 100
#define MAX_COL_LENGTH 256

Then you can use:

char b[MAX_ROW_LENGTH][MAX_COL_LENGTH];
Hitokiri
  • 3,607
  • 1
  • 9
  • 29
  • Unless the OP wants each index to be a variable-length string, there is really no need for the `char**` trick. It seems they want actual 2D arrays, so they shouldn't be using some `char**` heap fragmentation fiasco then. https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays – Lundin May 20 '20 at 13:08
  • i got it. Can i add your link to the answer ? – Hitokiri May 20 '20 at 13:15
  • You can, but we really shouldn't teach people to use this method unless they actually need it. Like when allocating a 1D array of pointers to strings of individual lengths. – Lundin May 20 '20 at 13:21