鉴于以下计划,
#include <iostream>
using namespace std;
void foo( char a[100] )
{
cout << "foo() " << sizeof( a ) << endl;
}
int main()
{
char bar[100] = { 0 };
cout << "main() " << sizeof( bar ) << endl;
foo( bar );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出
main() 100
foo() 4
Run Code Online (Sandbox Code Playgroud)
以下代码
#include <iostream>
using namespace std;
int main()
{
const char* const foo = "f";
const char bar[] = "b";
cout << "sizeof(string literal) = " << sizeof( "f" ) << endl;
cout << "sizeof(const char* const) = " << sizeof( foo ) << endl;
cout << "sizeof(const char[]) = " << sizeof( bar ) << endl;
}
Run Code Online (Sandbox Code Playgroud)
输出
sizeof(string literal) = 2
sizeof(const char* const) = 4
sizeof(const char[]) = 2
Run Code Online (Sandbox Code Playgroud)
在32位操作系统上,使用GCC编译.
sizeof计算字符串文字的长度(所需空间)?sizeof?