我尝试在Linux和VS 2008中编译以下代码:
#include <iostream> // this line has a ".h" string attached to the iostream string in the linux version of the code
using namespace std; // this line is commented in the linux version of the code
void 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 expression
1>c:\users\bibin\documents\visual studio 2008\projects\add\add\hello.cpp(7) : error C2466: cannot allocate an array of constant size 0
1>c:\users\bibin\documents\visual studio 2008\projects\add\add\hello.cpp(7) : error C2133: 'arr' : unknown size
这是一个有效的语句吗?两个编译器怎么会对同一种语言有不同的解释呢?