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.

asked Jul 30, 2020 at 20:56
6
  • 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. Commented Jul 30, 2020 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. Commented Jul 30, 2020 at 20:58
  • 5
    Why do you want to do this? std::strings already use arrays under the hood. Use .c_str() go get the pointer to its first element (or .data() for a writeable pointer). Commented Jul 30, 2020 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. Commented Jul 30, 2020 at 21:00
  • Related: https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard Commented Jul 30, 2020 at 21:04

2 Answers 2

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
607k36 gold badges515 silver badges870 bronze badges
answered Jul 30, 2020 at 20:58
1
  • 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> char_array(lenght + 1); std::copy(UserImput.begin(), UserImput.end(), char_array.begin()); std::cout << char_array.data(); Commented Jul 30, 2020 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();
answered Jul 30, 2020 at 21:40
2
  • 2
    Or better, cout << UserImput; since operator<< has an overload for std::string Commented Jul 30, 2020 at 22:16
  • 1
    Side note: Remember that the buffer returned by strdup needs to be freeed, not deleteed. Commented Jul 30, 2020 at 23:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.