代码之家  ›  专栏  ›  技术社区  ›  Chris Gray

为什么具有单独字符的字符数组不像字符串文字那样以空终止符结尾?

  •  0
  • Chris Gray  · 技术社区  · 8 年前

    我在c++中使用char数组,并编写了以下程序:

    int main()
    {
    
    char text[] = { 'h', 'e', 'l', 'l', 'o' };  //arrays initialised like this 
                                                //will have a size of the number 
                                                //of elements that you see
    
    char text2[] = "hello"; //arrays initialised like this will have a size of 
                            //the number of elements that you see + 1 (0 on the 
                            //end to show where the end is
    
    cout << endl;
    
    cout << "The size of the first array is: " << sizeof(text) << endl;
    
    cout << endl;
    
    for (int i = 0; i < sizeof(text); i++)
    {
        cout << i << ":" << text[i] << endl;
    }
    cout << endl;
    
    cout << "The size of the first array is: " << sizeof(text2) << endl;
    
    cout << endl;
    
    for (int i = 0; i < sizeof(text2); i++)
    {
        cout << i << ":" << text2[i] << endl;
    }
    cout << endl;
    
    cin.get();
    
    return 0;
    }
    

    该程序提供以下输出:

    The size of the first array is: 5
    
    0:h
    1:e
    2:l
    3:l
    4:o
    
    The size of the first array is: 6
    
    0:h
    1:e
    2:l
    3:l
    4:o
    5:
    

    我的问题是:与使用字符串文字初始化char数组不同,使用单独的char初始化char数组时,是否有一个特殊的原因使其末尾没有空终止符(0)?

    6 回复  |  直到 8 年前
        1
  •  4
  •   Bathsheba    8 年前

    大括号初始值设定项仅为数组提供指定的值(或者如果数组较大,则默认为其余项)。即使项目是 char 价值观 烧焦 是最小的整数类型。

    字符串文字表示以零结尾的值序列。

    仅此而已。

        2
  •  1
  •   Bathsheba    8 年前

    非正式地说,它是表单字符串文字中的第二个引号字符 "foo" 这将添加NUL终止符。

    在C++中, “foo” 是一个 const char[4] 类型,其中 衰变 到a const char* 在某些情况下。

    这就是语言的工作原理,仅此而已。它非常有用,因为它与所有标准库函数都非常吻合,这些函数将字符串建模为指向以NUL结尾的数组中第一个元素的指针 char s

    在一个额外的元素中拼接 char text[] = { 'h', 'e', 'l', 'l', 'o' }; 可能是 真正地 这很烦人,而且会给语言带来不一致。你会为我做同样的事吗 signed char unsigned char 例如那怎么办 int8_t ?

        3
  •  1
  •   Drew Dormann    8 年前

    是否有特殊原因导致使用单独的字符初始化字符数组时不会有空终止符(0)

    原因是因为语法。。。

    Type name[] = { comma separated list };
    

    。。。用于初始化阵列 任何类型的 .不仅仅是 char

    这个 "quoted string" 语法是假定需要空终止符的非常特定类型数组的简写。

        4
  •  0
  •   Benjamin Lindley    8 年前

    当您指定一组以双引号分隔的相邻字符(字符串文字)时,假定您想要的是字符串。C中的字符串表示以null结尾的字符数组,因为这是对字符串进行操作的函数( printf ,则, strcpy 等……)预料因此编译器会自动为您添加空终止符。

    当您提供一个以大括号分隔、逗号分隔的单引号分隔字符列表时,假定您不需要字符串,但需要一个与指定字符完全相同的数组。因此没有添加空终止符。

    C++继承了这种行为。

        5
  •  0
  •   Vlad from Moscow    8 年前

    字符串文字,例如 "hello" 具有常量字符数组的类型,并按以下方式初始化

    const char string_literal_hello[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
    

    正如所见,字符串文字的类型是 const char[6] 。它包含六个字符。

    因此,本声明

    char text2[] = "hello"; 
    

    也可以这样写

    char text2[] = { "hello" }; 
    

    事实上,以下声明已被替换

    char text2[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
    

    也就是说,字符串文字用作字符数组的初始值设定项,其所有字符都用于初始化数组。

        6
  •  0
  •   Maxim Egorushkin    8 年前

    您可以通过多种方式自行终止:

    char text1[6] = { 'h', 'e', 'l', 'l', 'o' };
    char text2[sizeof "hello"] = { 'h', 'e', 'l', 'l', 'o' };
    char text3[] = "hello"; // <--- my personal favourite