为什么“字符数组的初始化字符串太长"?在 C & 中编译得很好不在 C++ 中?

Why quot;initializer-string for array of chars is too longquot; compiles fine in C amp; not in C++?(为什么“字符数组的初始化字符串太长?在 C amp; 中编译得很好不在 C++ 中?)
本文介绍了为什么“字符数组的初始化字符串太长"?在 C & 中编译得很好不在 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++ 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)