C/C++中的sizeof
代码:
1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 int main(){ 7 char s1[]="hello"; 8 char *s2="hello"; 9 cout<<sizeof(s1)<<" "<<sizeof(s2)<<endl; 10 11 return 0; 12 }
输出:
6 8
分析:
sizeof后面跟数组,需要计算数组长度,如果是char型数组还要计算'\0'。