Initialize array with a non-const function argument
问题 Is there any way to initialize an array with a non-const integer, or to make the existing variable constant in order to make it a valid argument? bool f( const char s[], const int n ) { char c[n]; // error: expression must have a constant value } 回答1: No, not in the general case. Use vector<char> c(n) instead. Simplified, almost correct explanation: if you don't know what n is at compile time, neither does the compiler. So it cannot put aside memory for the array. This is why vector exists.