我知道这种方法char a[] = "Hello World";
有效,但是我想知道是否可以通过在运行时输入字符串来初始化字符数组。例如:
string word;
cout << "Enter a word ";
cin >> word;
char a[] = word;
我知道clang ++不接受它,因为它说:“数组初始值设定项必须是初始值设定项列表或字符串文字”。
是否可以解决此问题而不使用指针?
使用c_str
。
http://en.cppreference.com/w/cpp/string/basic_string/c_str
并将类似的内容复制到数组:
strcpy(a, word.c_str());
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句