1

I was making a simple program in c++ to convert a string to a char array and then print it out. My code is:

string UserImput;
int lenght;

 
 void Test()
{
     getline(cin, UserImput);
     
     lenght = UserImput.size();

     char char_array[lenght + 1];
     copy(UserImput.begin(), UserImput.end(), char_array);

    
     cout << char_array;
   
}

The error I am getting is "expression must have a costant value" and I do not know why.

Jozese
  • 33
  • 4
  • 4
    variable length arrays are not allowed in c++. The size of an array must be known at compile time, you aren't allowed to calculate it from run-time input. – cigien Jul 30 '20 at 20:57
  • 2
    Or you need to use new/delete to create your runtime sized buffer. You will also need to add a NUL terminator to your char array to print it correctly after your copy, unless you print it manually 1 character at a time. – Michael Dorgan Jul 30 '20 at 20:58
  • 5
    *Why* do you want to do this? `std::string`s already use arrays under the hood. Use `.c_str()` go get the pointer to its first element (or `.data()` for a writeable pointer). – HolyBlackCat Jul 30 '20 at 20:59
  • 1
    Variable Length Arrays are optionally allowed in C programs and by by extension by some C++ compilers, notably g++, so don't be surprised when you see them in examples and tutorials. Just don't expect the example to work under your compiler. – user4581301 Jul 30 '20 at 21:00
  • Related: [https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – drescherjm Jul 30 '20 at 21:04
  • @Jozese Also note that `new[]` does not zero memory, and a `std::string`'s iterators do not include a null-terminator, so even though your `char_array` is allocating space for a null-terminator, it will not actually contain a null-terminator, so `cout << char_array;` will likely run past the end of the allocated memory. So, you will need to add `char_array[lenght] = '\0';` after the `copy()`. Or, just don't use a `char[]` array to begin with. – Remy Lebeau Jul 30 '20 at 22:20

2 Answers2

5

In char char_array[lenght + 1];, lenght + 1 is not a compile-time constant. The size of an array must be known at compile-time. Since the value of length is not known until runtime, you will need to allocate memory dynamically in this situation. For example:

char* char_array = new char[lenght + 1];

Don't forget to delete the array when you are done:

delete[] char_array;

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Andy
  • 12,859
  • 5
  • 41
  • 56
  • Rather than using `new[]` directly, you should use `std::vector` instead, then you don't have to worry about calling `new[]`/`delete[]` manually, eg: `std::vector char_array(lenght + 1); std::copy(UserImput.begin(), UserImput.end(), char_array.begin()); std::cout << char_array.data();` – Remy Lebeau Jul 30 '20 at 22:17
0

You can use:

  char* c = _strdup(UserImput.c_str());

But I wonder - why? Just to output it to cout? Then you can do:

cout << UserImput.c_str();
Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27