1.sizeof
- int,int* ,int[ ]
- char, char*,char[ ]
- 数据类型
char charTest1 = 'a';
char charTest2[10] = "Test";
cout << "sizeof(char): \n";
cout << "sizeof(charTest1)= " << sizeof charTest1 << endl;
cout << "sizeof(char[]):\n";
cout << "sizeof(charTest2)= " << sizeof charTest2 << endl;
cout << "sizeof(char*):\n";
cout << "sizeof(&charTest2[0])= " << sizeof & charTest2[0] << endl;
int intTest1 = 1;
int intTest2[10] = { 1,2,3,4,5 };
cout << "sizeof(int): \n";
cout << "sizeof(intTest1)= " << sizeof intTest1 << endl;
cout << "sizeof(int[]):\n";
cout << "sizeof(intTest2)= " << sizeof intTest2 << endl;
cout << "sizeof(int*):\n";
cout << "sizeof(&intTest2[0])= " << sizeof & intTest2[0] << endl;
int* ptr = new int[10];
cout << sizeof ptr << endl;
delete[] ptr;
- sizeof 不是函数,而是操作符,因此可以对数据类型操作
- sizeof 返回数组的容量大小,而不是元素的个数
- sizeof操作符的结果类型是size_t,它在头文件中typedef为unsigned int类型
2.strlen()
const char* charTest3 = "TestStrlen";
cout << "strlen(char[]):\n";
cout << "strlen(charTest2)= " << strlen(charTest2) << endl;
cout << "strlen(char*):\n";
cout << "strlen(charTest3)= " << strlen(charTest3) << endl;
- strlen( )返回元素的个数
- strlen( )遇到’\0’停止,并且’\0’不计数
- strlen( )返回逻辑个数