我如何通過一個char向量爲char *?我知道這個問題很容易用一個帶有SIZE常量的預定義的char []數組來解決,但我想要一個矢量的靈活性,因爲沒有預定義的大小。傳遞載體<char>的指針的char *
using namespace std;
//prototype
void getnumberofwords(char*);
int main() {
//declare the input vector
vector<char> input;
/*here I collect the input from user into the vector, but I am omitting the code here for sake of brevity...*/
getnumberofwords(input);
//here is where an ERROR shows up: there is no suitable conversion from std::vector to char*
return 0;
}
void getnumberofwords(char *str){
int numwords=0;
int lengthofstring = (int)str.size();
//this ERROR says the expression must have a case
//step through characters until null
for (int index=0; index < lengthofstring; index++){
if (*(str+index) == '\0') {
numwords++;
}
}
}
-1:錯誤的代碼格式,實際上不是一個答案原來的問題。 – Griddo 2014-07-31 08:28:03