<bdo id='lCT5C'></bdo><ul id='lCT5C'></ul>
  1. <i id='lCT5C'><tr id='lCT5C'><dt id='lCT5C'><q id='lCT5C'><span id='lCT5C'><b id='lCT5C'><form id='lCT5C'><ins id='lCT5C'></ins><ul id='lCT5C'></ul><sub id='lCT5C'></sub></form><legend id='lCT5C'></legend><bdo id='lCT5C'><pre id='lCT5C'><center id='lCT5C'></center></pre></bdo></b><th id='lCT5C'></th></span></q></dt></tr></i><div id='lCT5C'><tfoot id='lCT5C'></tfoot><dl id='lCT5C'><fieldset id='lCT5C'></fieldset></dl></div>
    <tfoot id='lCT5C'></tfoot>

      <small id='lCT5C'></small><noframes id='lCT5C'>

    1. <legend id='lCT5C'><style id='lCT5C'><dir id='lCT5C'><q id='lCT5C'></q></dir></style></legend>

      为什么“字符数组的初始化字符串太长"?在

      时间:2023-09-18

      <tfoot id='x2Dl5'></tfoot>

      <small id='x2Dl5'></small><noframes id='x2Dl5'>

      <i id='x2Dl5'><tr id='x2Dl5'><dt id='x2Dl5'><q id='x2Dl5'><span id='x2Dl5'><b id='x2Dl5'><form id='x2Dl5'><ins id='x2Dl5'></ins><ul id='x2Dl5'></ul><sub id='x2Dl5'></sub></form><legend id='x2Dl5'></legend><bdo id='x2Dl5'><pre id='x2Dl5'><center id='x2Dl5'></center></pre></bdo></b><th id='x2Dl5'></th></span></q></dt></tr></i><div id='x2Dl5'><tfoot id='x2Dl5'></tfoot><dl id='x2Dl5'><fieldset id='x2Dl5'></fieldset></dl></div>
      • <legend id='x2Dl5'><style id='x2Dl5'><dir id='x2Dl5'><q id='x2Dl5'></q></dir></style></legend>

          <tbody id='x2Dl5'></tbody>
              <bdo id='x2Dl5'></bdo><ul id='x2Dl5'></ul>
                本文介绍了为什么“字符数组的初始化字符串太长"?在 C &amp; 中编译良好不是在 C++ 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                以下程序在 C 中编译正常,但有警告,但在 C++ 中编译失败.为什么?是什么原因?

                Following program compiles fine in C with warnings but fails in compilation in C++. Why? What is the reason?

                #include <stdio.h>
                int main(void)
                {
                    char a[5]="Hello";
                    a[0]='y';
                    puts(a);
                    for(int i=0;i<5;i++)
                        printf("%c",a[i]);
                    return 0;
                }
                

                警告:

                Warning:[Error] initializer-string for array of chars is too long [-fpermissive] enabled by default
                

                但是如果程序被编译为 C++ 程序,那么 C++ 编译器会出现以下错误:

                But if the program is compiled as C++ program then C++ compiler gives following error:

                [Error] initializer-string for array of chars is too long [-fpermissive]
                

                我使用的是 GCC 4.8.1 编译器.

                I am using GCC 4.8.1 compiler.

                推荐答案

                简短回答:因为 C 和 C++ 是不同的语言,具有不同的规则.

                Short answer: Because C and C++ are different languages with different rules.

                长答案:在这两种情况下,原因都是数组对于字符串文字来说太小了.文字由五个可见字符组成,末尾有一个零终止符,因此总大小为 6.

                Long answer: In both cases the reason is that the array is too small for the string literal. The literal consists of the five visible characters, with a zero terminator on the end, so the total size is 6.

                在 C 中,您可以使用太长的字符串初始化数组;额外的字符会被忽略:

                In C, you're allowed to initialise an array with a string that's too long; extra characters are simply ignored:

                C99 6.7.8/14:字符类型的数组可以由字符串文字初始化,可选地用大括号括起来.字符串文字的连续字符(如果有空间或数组大小未知,则包括终止空字符)初始化数组的元素.

                C99 6.7.8/14: An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array.

                编译器会警告字符串太大,因为它几乎可以肯定表示有错误;但除非您告诉它将警告视为错误,否则它无法拒绝代码.

                The compiler helpfully warns that the string is too large, since it almost certainly indicates an error; but it can't reject the code unless you tell it to treat warnings as errors.

                在 C++ 中,初始化器不允许大于数组:

                In C++, the initialiser isn't allowed to be larger than the array:

                C++11 8.5.2/2:初始化器的数量不应多于数组元素.

                C++11 8.5.2/2: There shall not be more initializers than there are array elements.

                所以,对于那种语言,编译器应该给出一个错误.

                so, for that language, the compiler should give an error.

                在两种语言中,当您希望字符数组的大小适合字符串字面量初始化程序时,您可以不考虑大小,编译器会做正确的事情.

                In both languages, when you want a character array to be the right size for a string literal initialiser, you can leave the size out and the compiler will do the right thing.

                char a[] = "hello";  // size deduced to be 6
                

                这篇关于为什么“字符数组的初始化字符串太长"?在 C &amp; 中编译良好不是在 C++ 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:检测二进制文件的 GCC 编译时标志 下一篇:算术右移给出虚假结果?

                相关文章

                最新文章

                <tfoot id='KW9Zx'></tfoot>
              1. <legend id='KW9Zx'><style id='KW9Zx'><dir id='KW9Zx'><q id='KW9Zx'></q></dir></style></legend>
              2. <i id='KW9Zx'><tr id='KW9Zx'><dt id='KW9Zx'><q id='KW9Zx'><span id='KW9Zx'><b id='KW9Zx'><form id='KW9Zx'><ins id='KW9Zx'></ins><ul id='KW9Zx'></ul><sub id='KW9Zx'></sub></form><legend id='KW9Zx'></legend><bdo id='KW9Zx'><pre id='KW9Zx'><center id='KW9Zx'></center></pre></bdo></b><th id='KW9Zx'></th></span></q></dt></tr></i><div id='KW9Zx'><tfoot id='KW9Zx'></tfoot><dl id='KW9Zx'><fieldset id='KW9Zx'></fieldset></dl></div>

                  • <bdo id='KW9Zx'></bdo><ul id='KW9Zx'></ul>

                  <small id='KW9Zx'></small><noframes id='KW9Zx'>