why it's not possible to create an char array in this way?
int main()
{
int len;
cin>>len;
char input_str[len];
cin>>input_str;
cout<<input_str;
return 0;
}
why it's not possible to create an char array in this way?
int main()
{
int len;
cin>>len;
char input_str[len];
cin>>input_str;
cout<<input_str;
return 0;
}
You cannot use a static array without knowing the size of the array at runtime. Instead you can use a pointer and use either "malloc" or "new" to allocate memory for the array dynamically:
1) First check that the user has entered a valid int
2) Once you have a valid int to work with, you can use...
char *input_str = new char[len];
for C++ or if you have to stick with plain old C use ...
char *input_str = (char *)malloc(len * sizeof(char));