0

I've written a little thing which asks the user for some input (rows and cols), which should then set everything in an array to a dot (".") and print it out, but this crashes my application.

void main()
{


    int i,j, m, n;

          printf("The number of lines (m): ");
          scanf("%d", m );
          printf("\nThe number of columns (n): ");
          scanf("%d", n);


    //create my array

    char mineGrid[n][m];

    //set all fields in to safe (.)

    for (j = 0; j <= n; j++)
    {
       for (i = 0; i <= m; i++)
          {
             mineGrid[j][i] = ".";
          }
    }
   //print a grid of dots

    for (j = 0; j <= n; j++)
    {
       for (i = 0; i <= m; i++)
          {
             printf("%s", mineGrid[j][i]);
          }
    }
}

Any idea why this is crashing?

4 Answers 4

2

On cause of major trouble here is that you have a lot of loop that look like

for (j = 0; j <= n; j++) 
/*             ^      */
/*             |      */
/*           Look!    */

which will run j from 0 to n, but you have declared your array as

char mineGrid[n][m];

which means that space has been allocated for rows numbered 0 to n-1.

All you index loops are wrong in that way. The idomatic way to write those loops is

for (j = 0; j < n; ++j) 

where I have fixed the range and also changed the increment from post- to pre- which is an old micro-optimization that generally does not make any difference in c these days (because compilers are smart enough to fix it), but can if you switch to c++ and use a non-trivial class in that way. So I keep it in my list of little things to "fix".

Sign up to request clarification or add additional context in comments.

1 Comment

Also cheers!, I was going a bit mad there for a second! Was just about to plot it with pen & paper.
0

That's because you're putting a string in the array instead of char.

do it like this:

void main()
{


    int i,j, m, n;

    m = 5;
    n = 6;



    //create my array

    char mineGrid[n][m];

    //set all fields in to safe (.)

    for (j = 0; j <= n; j++)
    {
        for (i = 0; i <= m; i++)
        {
            mineGrid[j][i] = '.';
        }
    }
    //print a grid of dots

    for (j = 0; j <= n; j++)
    {
        for (i = 0; i <= m; i++)
        {
            printf("%c", mineGrid[j][i]);
        }
        printf("\n");
    }
}

2 Comments

I don't think you are correct. You are thinking m,n are constants after assignment. But they are not. Variable Length Arrays are allowed from C99 standard.
Derr. Looked at that for a while. Thanks a lot!
0

you created n X m elements but used n+1 X m+1 elements in array. use like bellow

a

for (j = 0; j < n; j++)
{
    for (i = 0; i < m; i++)
    {
        mineGrid[j][i] = '.';
    }
}

Comments

0

That's because for an array of size N, the valid array indexes 0 to N-1. But you are accessing N th element which is not a valid array index and accessing it invokes undefined behavior.

for (j = 0; j <= n; j++)
{
   for (i = 0; i <= m; i++) // Array out of bounds in either condition check

With that said, you have issues with your input as well.

scanf("%d", m ); // Missing & operator before m.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.