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

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?

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.

I am using 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;

}

Ed S.
  • 122,712
  • 22
  • 185
  • 265
Giuseppe
  • 490
  • 1
  • 11
  • 27
  • Retagging as C since the code seems to be so. – Mike Kwan Mar 27 '12 at 00:32
  • 3
    @MikeKwan: Guys, don't retag as C because he is using a C++ compiler (Visual Studio without explicitly compiling as C code), and the problem is not a zero sized array, that's just the second error message you see. – Ed S. Mar 27 '12 at 00:36
  • Ed is correct, when you attempt to compile code like this in Visual Studio you will first see the constant expression error and then the zero size error. – ShiggityShiggityShwa Mar 27 '12 at 00:38
  • 1
    Don't know why anyone would vote to close this, it's a perfectly valid question (and a problem understandably confusing to a beginner). – Ed S. Mar 27 '12 at 00:46
  • Sorry guys , but as i told you on the description my english is not that cientific. Im not an english-speaker so i dont really know the "words" to explain my problems. Btw, ed it worked now, but i got problems reading the input. – Giuseppe Mar 27 '12 at 00:55
  • 1
    @Giuseppe: Please feel free to resolve this thread by selecting an answer and posting a new thread regarding your current problem (assuming you can't find an existing thread that covers it) – Ed S. Mar 27 '12 at 16:41

1 Answers1

10
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.

error C2057: expected constant expression

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).

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.)

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • oh now i understand it , well i will try to create it dynamically. And see what happens cause i wanna see the input problem too . Thanks for your time – Giuseppe Mar 27 '12 at 00:37
  • 1
    This is legal in the newer versions of C. – Mateen Ulhaq Mar 27 '12 at 00:42
  • 1
    @muntoo: Right, but he is using VS which does not support C99 and, even though this *looks* like C code to us, I can almost guarantee you that he is compiling as C++. So either way, this is not legal. – Ed S. Mar 27 '12 at 00:43
  • Note that you can workaround this limitation with `alloca` – Zaffy Feb 21 '20 at 15:10