C编译器差异(VS2008和g)

C编译器差异(VS2008和g),第1张

概述我尝试在 Linux和VS 2008中编译以下代码: #include <iostream> // this line has a ".h" string attached to the iostream string in the linux version of the codeusing namespace std; // this line is commented in the li 我尝试在 Linux和VS 2008中编译以下代码:
#include <iostream> //  this line has a ".h" string attached to the iostream string in the linux version of the codeusing namespace std; // this line is commented in the linux version of the codevoID main(){  int a=100;  char arr[a];  arr[0]='a';  cout<<"array is:"<<arr[0];}

此行适用于g版本,但在Visual Studio中不起作用.
它会引发以下错误:

1>c:\users\bibin\documents\visual studio 2008\projects\add\add\hello.cpp(7) : error C2057: expected constant Expression1>c:\users\bibin\documents\visual studio 2008\projects\add\add\hello.cpp(7) : error C2466: cannot allocate an array of constant size 01>c:\users\bibin\documents\visual studio 2008\projects\add\add\hello.cpp(7) : error C2133: 'arr' : unkNown size

这是一个有效的陈述吗?两个编译器如何对同一语言有不同的解释

解决方法 这是C99功能:
char arr[a]; // VLA: Variable Length Arrays (C99) but not C++!

GCC支持C99的许多功能,但VC没有,我认为它不会在不久的将来,因为他们越来越专注于C.无论如何,您只需将声明更改为:

const int a=100; // OK the size is const Now!  char arr[a];
总结

以上是内存溢出为你收集整理的C编译器差异(VS2008和g)全部内容,希望文章能够帮你解决C编译器差异(VS2008和g)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1236791.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存