C / C ++编译器的结果不同 [英] different results for C/C++ compiler

查看:105
本文介绍了C / C ++编译器的结果不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char s [7] =" 1234567";


当我们使用C编译器(gcc)时,它工作正常。

错误是报道是否使用C ++编译器(g ++)。


这个故事的任何故事?


Vol

char s[7] = "1234567";

when we use C compiler (gcc), it works fine.
An error is reported if using C++ compiler (g++).

any story for this one?

Vol

推荐答案

Vols写道:
Vols wrote:

char s [7] =" 1234567" ;;
char s[7] = "1234567";



这是一个错误,因为你没有

结束数组为零你把7

字符放在一个7位数组中,然后是字符串

不是零终止。


在C中是合法的,在C ++中是非法的。

This is an error since you do not
end the array with zero You put 7
chars, in a 7 position array, then the string
is not zero terminated.

legal in C, illegal in C++.


当我们使用C编译器(gcc)时,它工作正常。

如果使用C ++编译器(g ++),则报告错误。


任何故事这一个?


Vol
when we use C compiler (gcc), it works fine.
An error is reported if using C++ compiler (g++).

any story for this one?

Vol




-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique
http://www.cs.virginia.edu/~lcc-win32


Vols写道:
Vols wrote:

char s [7] =" 1234567";


当我们使用C编译器(gcc)时,它工作正常。

如果使用C ++编译器(g ++),则报告错误。

这个故事的任何故事?
char s[7] = "1234567";

when we use C compiler (gcc), it works fine.
An error is reported if using C++ compiler (g++).

any story for this one?



初始化程序过多。在C ++中,数组是从字符串文字的

数组初始化的。字符串文字1234567有

8(八)个字符,数字和终止零。

C ++不允许为数组元素提供更多初始化程序

比数组大小。放大小,只需要做好


char s [] =" 1234567" ;;


或将其更改为8.


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Too many initialisers. In C++, the array is initialised from the
array that is the string literal. The string literal "1234567" has
8 (eight) characters in it, the numbers and the terminating zero.
C++ does not allow to provide more initialisers to array elements
than the array size. Drop the size, just do

char s[] = "1234567";

or change it to 8.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


我问这个问题的原因是:为什么它在C中合法?

所以我们不需要在C中以0结尾?

谢谢。


Vol

4月24日上午10:30,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
The reason I asked this question is: why is it legal in C?
So we don''t need to end with ''0'' in C?
Thanks.

Vol
On Apr 24, 10:30 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

Vols写道:
Vols wrote:

char s [7] =" 1234567" ;;
char s[7] = "1234567";


当我们使用C编译器(gcc)时,它工作正常。

如果使用C ++编译器,则报告错误(克++)。
when we use C compiler (gcc), it works fine.
An error is reported if using C++ compiler (g++).


这个故事的任何故事?
any story for this one?



初始化程序过多。在C ++中,数组是从字符串文字的

数组初始化的。字符串文字1234567有

8(八)个字符,数字和终止零。

C ++不允许为数组元素提供更多初始化程序

比数组大小。放大小,只需要做好


char s [] =" 1234567" ;;


或将其更改为8.


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


Too many initialisers. In C++, the array is initialised from the
array that is the string literal. The string literal "1234567" has
8 (eight) characters in it, the numbers and the terminating zero.
C++ does not allow to provide more initialisers to array elements
than the array size. Drop the size, just do

char s[] = "1234567";

or change it to 8.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于C / C ++编译器的结果不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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