相关疑难解决方法(0)

Sizeof字符串文字

以下代码

#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编译.

  1. 为什么要sizeof计算字符串文字的长度(所需空间)?
  2. 当给出时,字符串文字是否具有不同的类型(来自char*或char [])sizeof

c++ string sizeof

46
推荐指数
2
解决办法
4万
查看次数

标签 统计

c++ ×1

sizeof ×1

string ×1