以下代码
#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?