#include<iostream>
using namespace std;
int main()
{
char arr[200];
while(1) {
cin >> arr;
int i = sizeof(arr);
cout << "The arr input is "<< arr
<< " and the size of the array is "<< i << endl;
}
return 0;
}
對於34輸入, 該代碼輸出:ARR的輸入是34和陣列的大小是200如何獲得數組中已用空間的大小? (NOT sizeof); C++
而我希望它得到的已用空間的大小陣列。因此,對於最後輸入我希望它輸出:ARR的輸入是34和數組的大小是2
誰能告訴我怎麼樣?
注意,你的代碼會爆炸,如果有人進入超過199個字符。另一個在C++代碼中使用'std :: string'優先於舊式C-string的原因。 – 2010-02-10 00:18:11