Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将1添加到字符串文字的语句中的差异_C++_String - Fatal编程技术网

C++ 将1添加到字符串文字的语句中的差异

C++ 将1添加到字符串文字的语句中的差异,c++,string,C++,String,我一直在做下面的作业来解释3个语句中发生了什么,但我想不出来 cout << ("hello" + 1); // ello cout << (*"hello") + 1; // 105 cout << (*("hello" + 1)); // e cout 您可以在此处取消引用一个const char[]。第一个字符是h,带有ascii码104。添加一个,您将获得105 cout << (*("hello" + 1)); // e c

我一直在做下面的作业来解释3个语句中发生了什么,但我想不出来

cout << ("hello" + 1);    // ello
cout << (*"hello") + 1;   // 105
cout << (*("hello" + 1)); // e
cout
您可以在此处取消引用一个
const char[]
。第一个字符是h,带有ascii码
104
。添加一个,您将获得
105

cout << (*("hello" + 1)); // e
cout“hello”是
const char*

  • 为什么2是数字->*“hello”将是基址处的值,即h(104)的ascii值,所以104+1=105

  • 是的,您刚才指向的是
    e
    ,而不是
    h

  • *“hello”
    给出字符串的第一个字符
    'h'
    ,类型为
    char
    ,ASCII值为104。整数提升规则意味着,当添加
    char
    int
    时,
    char
    转换为
    int
    ,给出类型为
    int
    的结果。输出
    int
    给出数值

  • 对。字符串文字是以零字符结尾的数组。将一个添加到其地址会给出指向数组第二个字符的指针;数组的其余部分不变,因此在末尾仍然包含零


  • 值得注意的是,字符串文字是
    const char[N]
    s。不能取消对数组的引用。您取消引用数组衰减成的指针。
    “hello”
    const char[6]
    @chris string我猜是文本,所以它可以是const char*,但是
    const char[6]
    ?是的,您可以通过
    std::is_same
    自己看到。
    cout << (*"hello") + 1;   // 105
    
    cout << (*("hello" + 1)); // e