期望看到"初始化字符串的字符数组太长"警告 [英] Expected to see "initializer-string for array of chars is too long" warning

查看:213
本文介绍了期望看到"初始化字符串的字符数组太长"警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期望看到初始化字符串的字符数组是太长使用gcc下面的程序这两​​个变量的警告。

程序:

  INT的main()
{
   炭STR1 [4] =1234;
   炭STR2 [3] =1234;   (无效)STR1; //删除未使用变量警告。
   (无效)STR2; //删除未使用变量警告。
   返回0;
}

不过,我得到了一个警告,只为 STR2

由于

 字符STR1 [4] =1234;

等同于

 炭STR1 [4] = {'1','2','3','4','\\ 0'};

难道我们不应该得到 STR1 也?

同样的警告

这是海湾合作委员会中的一个缺陷?

编译器命令:


  

GCC -Wall -std = C99 soc.c -o SOC


GCC 的版本是4.8.4。

更新

据悉刚才说

 字符STR1 [4] =1234;

不是等同于

 炭STR1 [4] = {'1','2','3','4','\\ 0'};

更新2

 字符STR1 [4] =1234;

是非法的构造在C ++ 11(第8.5.2节/ 2)。我没想到C99和C ++ 11将区别对待。


解决方案

C标准第6.7.9规定:


  
  
  • 字符类型的数组,可以用一个字符串被初始化
      文字或UTF-8字符串,可选大括号括起来。
      该字符串的连续字节(包括终止
      空字符,如果有空间,或者如果数组是未知大小的

      初始化数组的元素。

  •   
      
      

    ...


      
      
      
  • 例8


      
      

    声明

     个char [] =ABC,T [3] =ABC;

    定义''纯'char数组对象S和T的元素用字符串文字进行初始化。这个声明是相同的。

     个char [] = {'A​​','B','C','\\ 0'},T [] = {'A​​','B',' C' };


  •   

    (重点煤矿)。

    即,如果它不适合入固定的已知尺寸数组不加入终止空字符。

    I expected to see "initializer-string for array of chars is too long" warning for both of the variables in the following program using gcc.

    Program:

    int main()
    {
       char str1[4]="1234";
       char str2[3]="1234";
    
       (void)str1; // Remove unused variable warning.
       (void)str2; // Remove unused variable warning.
       return 0;
    }
    

    However, I got a warning only for str2.

    Since

    char str1[4]="1234";
    

    is equivalent to

    char str1[4]= {'1', '2', '3', '4', '\0'};
    

    shouldn't we get the same warning for str1 also?

    Is this a defect in gcc?

    Compiler command:

    gcc -Wall -std=c99 soc.c -o soc

    gcc version is 4.8.4.

    Update

    Learned just now that

    char str1[4]="1234";
    

    is not equivalent to

    char str1[4]= {'1', '2', '3', '4', '\0'};
    

    Update 2

    char str1[4]="1234";
    

    is ill-formed in C++11 (Section 8.5.2/2). I didn't think C99 and C++11 would treat them differently.

    解决方案

    Section 6.7.9 of the C standard reads:

    1. An array of character type may be initialized by a character string literal or UTF −8 string literal, optionally enclosed in braces. Successive bytes of the 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.

    ...

    1. EXAMPLE 8

      The declaration

      char s[] = "abc", t[3] = "abc";
      

      defines ‘‘plain’’ char array objects s and t whose elements are initialized with character string literals. This declaration is identical to

      char s[] = { 'a', 'b', 'c', '\0' }, t[] = { 'a', 'b', 'c' };
      

    (Emphasis mine).

    That is, the terminating null character is not added if it does not fit into the fixed known size array.

    这篇关于期望看到"初始化字符串的字符数组太长"警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆