字符串的sizeof和strlen
sizeof,strlen的使用例子
#include< iostream
>
#include< string
>
using namespace
std;
int main()
{
char a[] =
"abcdef";
char b[20]
="abcdef";
string s =
"abcdef";
int d[20] = {3,
4};
char e[2][3] = { "aa",
"bb"};
cout <<strlen(a)
<<endl; // 6,字符串长度
cout <<sizeof(a)
<<endl; // 7,字符串容量
cout <<strlen(b)
<<endl; // 6,字符串长度
cout <<sizeof(b)
<<endl; // 20,字符串容量
cout <<sizeof(s)
<<endl; // 16, 这里不代表字符串的长度,而是string类的大小
//cout <<strlen(s)
<<endl; // 错误!s不是一个字符指针。
cout <<sizeof(d)
<<endl; // 20*4
cout <<sizeof(e)
<<endl; // 6
return 0;
}