0

I have this code:

int new_length = strlen(string);
char new_string[new_length];

So I assumed "new_length" should be constant, so now the code looks like this:

const int new_length = strlen(string);
char new_string[new_length];

And im still have the same Error.

"Expression must have constant value"

How can I fix it?

Mat
  • 202,337
  • 40
  • 393
  • 406
fdwfg wdfwdfv
  • 229
  • 1
  • 4
  • 9
  • Also http://stackoverflow.com/questions/9219712/c-array-expression-must-have-a-constant-value – Mat Jan 17 '17 at 16:21
  • @Mat No. its not the same. because here I declared it as constant, – fdwfg wdfwdfv Jan 17 '17 at 16:21
  • That variable is const, but its value is determined at runtime. The array length must be a compile-time constant in C++. BTW using `std::string` would probably avoid all those issues. – Mat Jan 17 '17 at 16:22
  • @Mat So in C compilers it should work? – fdwfg wdfwdfv Jan 17 '17 at 16:23
  • As long as the compiler supports at least C99 or has an extension for that. They are called VLAs in C. – Mat Jan 17 '17 at 16:24

0 Answers0