strlen作用是计数器的作用,计数字符串的长度,遇到\0停止计数。
sizeof作用是计算所占的字节数大小。如果是字符串的话,注意\0也是占了一个字节。
#include <iostream>
using namespace std;
void Func(char str[100])
{
cout << sizeof(str) << endl;
}
int main()
{
char str[] = "Hello";
//string str = "Hello World!";
cout << sizeof(str) << endl;
char* a = "123456";
char b[] = "123456";
cout << "strlen(a) = " << strlen(a) << " sizeof(a) = " << sizeof(a) << endl;
cout << "strlen(b) = " << strlen(b) << " sizeof(b) = " << sizeof(b) << endl;
char str1[] = "abc kajfla";
cout << "strlen(str1) = " << strlen(str1) << endl;
int arr1[] = { 1, 2, 3, 4 };
cout << "sizeof(arr1) = " << sizeof(arr1) << endl;
cout << endl;
system("pause");
return 0;
}