1、定義數組,要給定其長度,也可以用Type a[ ] = {……} 的方式。 在對數組進行操作時,可能需要計算數組長度,方法是:sizeof(數組名)/sizeof(元素類型)
2、指針指向的字符數組長度的獲取方法,不能用sizeof,因為用sizeof(指針),得到指針長度為4 應該用strlen()函數。
#include <iostream>#include <string>using namespace std;int num(char *ptr){ int bb = strlen(ptr); return bb;}int main(){ char *p= new char[100]; p = "string"; int b = num(p); cout<<b<<endl; return 0;}新聞熱點
疑難解答