This question already has an answer here:
这个问题已经有了答案:
- What happens if I define a 0-size array in C/C++? 7 answers
- 如果我在C/ c++中定义一个0大小的数组会发生什么?7的答案
I am doing a minesweeper program for school, but i keep getting this error on my code
我正在为学校做扫雷程序,但是我的代码一直有这个错误
cannot allocate an array of constant size 0
不能分配一个常数大小为0的数组。
I do not know why this is happening; I am not allocating the size -- I am setting that on 0. Another problem is, how can I read my input char
by char
, so I can save it in my array?
我不知道这是为什么;我没有分配大小,我把它设为0。另一个问题是,如何通过char读取输入字符,以便在数组中保存它?
As you can see below, I am using an input and output. I comented my input and my ouput so you guys can see what im using for this program. I want to read char by char
so I can save all the map on the array.
正如您在下面看到的,我正在使用输入和输出。我输入了输入和输出,这样你们就能看到我在用什么程序了。我想用char读取字符,这样就可以保存数组中的所有映射。
I am using MSVC++2010.
我用MSVC + + 2010。
freopen("input.txt","rt",stdin);
//4 4
//*...
//....
//.*..
//....
//3 5
//**...
//.....
//.*...
//0 0
freopen("output.txt","wt",stdout);
/*Field #1:
*100
2210
1*10
1110
Field #2:
**100
33200
1*100*/
int n=-1;
int m=-1;
int cont =0;
while(n!=0 && m!=0)
{
scanf("%d %d",&n,&m);
int VMatriz[n][m]={0};
int Mapa[n][m]={0};
if (n==0 && m==0)
break;
cont++;
printf("Field #%d",cont);
for (int i=0;i<n;i++)
{ printf("/n");
for (int j=0;j<m;j++)
{
scanf("%d ",&Mapa[i][j]);
if (Mapa[i][j]=='*')
{
if (j-1>=0)
VMatriz[i][j-1]++;
if (j+1<m)
VMatriz[i][j+1]++;
if (i-1>=0)
VMatriz[i-1][j]++;
if (i+1<m)
VMatriz[i+1][j]++;
if (j-1>=0 && i-1>0)
VMatriz[i-1][j-1]++;
if (j-1>=0 && i+1<m)
VMatriz[i+1][j-1]++;
if (j+1<m && i-1>0)
VMatriz[i-1][j+1]++;
if (j+1<m && i+1<m)
VMatriz[i+1][j+1]++;
VMatriz[i][j]='*';
printf("%d",VMatriz[i][j]);
}
}
}
printf("/n");
}
return 0;
}
}
1 个解决方案
#1
8
int VMatriz[n][m]={0};
This is illegal. As is this simpler version;
这是非法的。就像这个简单的版本;
int n = 10;
int x[n]; // C2057
However...
然而……
int x[10]; // ok!
The error you actually care about here is this one, not the "cannot allocate an array of constant size 0" error.
你真正关心的错误是这个,而不是“无法分配一个常量大小为0的数组”的错误。
error C2057: expected constant expression
错误C2057:期望常数表达式。
You cannot allocate an array of unknown size with automatic storage duration in C++. If you want a variable sized array then you need to dynamically allocate it (or, better yet; just use a vector
).
不能在c++中使用自动存储持续时间分配未知大小的数组。如果你想要一个可变大小的数组,那么你需要动态地分配它(或者更好;只使用一个向量)。
Note that there is a gcc extension to allow this, but not in VS (and it is not standard C++. It was submitted for C++ 11, but ultimately declined.)
请注意,有一个gcc扩展允许这样做,但是在VS中没有(它不是标准的c++)。它被提交给c++ 11,但最终被拒绝了。
#1
8
int VMatriz[n][m]={0};
This is illegal. As is this simpler version;
这是非法的。就像这个简单的版本;
int n = 10;
int x[n]; // C2057
However...
然而……
int x[10]; // ok!
The error you actually care about here is this one, not the "cannot allocate an array of constant size 0" error.
你真正关心的错误是这个,而不是“无法分配一个常量大小为0的数组”的错误。
error C2057: expected constant expression
错误C2057:期望常数表达式。
You cannot allocate an array of unknown size with automatic storage duration in C++. If you want a variable sized array then you need to dynamically allocate it (or, better yet; just use a vector
).
不能在c++中使用自动存储持续时间分配未知大小的数组。如果你想要一个可变大小的数组,那么你需要动态地分配它(或者更好;只使用一个向量)。
Note that there is a gcc extension to allow this, but not in VS (and it is not standard C++. It was submitted for C++ 11, but ultimately declined.)
请注意,有一个gcc扩展允许这样做,但是在VS中没有(它不是标准的c++)。它被提交给c++ 11,但最终被拒绝了。