0

I was trying to test some things out on an online compiler regarding arrays. I have been having an error that states that my array is variably modified. How do I initialize the array while still having variables inside the array that I can use within my cells? Am I on the right path with the use of isSetup. A friend gave me that advice. How would you go about this. The exact error happened when I defined my global array int myrowcol[col][row]; at line 3.

int row;
int col;
int myrowcol[col][row];
int const team = 1;
int rowmux;

void main()
{

    if (isSetup == 0)
    {
        row = 7;
        col = 7;
        myrowcol[row][col];
        isSetup = 1;
    }
    for (int i = 0; i < 64; i ++)
    {
        for (int col = 7; col > 0; col--)
        {
            for (int row = 7; row > 0; row--)
            {
                if (myrowcol[col][row - 1] == 1 || row == 0)
                {
                    myrowcol[col][row] = team;
                }
            }
        }
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ryan Paye
  • 21
  • 4
  • 1
    `int myrowcol[col][row];` The `row` and `col` variables have not been set at that point and have indeterminate values. – kaylum Jul 31 '22 at 23:43
  • 1
    `myrowcol[col][row] = team;` That would be a buffer overflow as array indices are 0 based in C. An array of `col` entries has max index `col-1`. Same for `row.` – kaylum Jul 31 '22 at 23:44
  • All executable C code and all declarations of variably-modified types (of which variable-length arrays are the prime example) must appear *inside functions*. The error message quoted in the title indicates that you have one at file scope, which means outside any function. The `if` statement is not allowed at file scope either, but that's irrelevant to the message you are asking about. – John Bollinger Jul 31 '22 at 23:50
  • @kaylum Thanks for putting that to my attention with the zero index. To clarify to your first comment, I am trying to use row and col as markers to where to fill the array cells. How will I initialize an array to do that? – Ryan Paye Jul 31 '22 at 23:51
  • Your code suggests that the dimensions are not actually variable, in that the inner two loops runh between two constant indexes. If the array dimensions are supposed to be variable, then those constant loop bounds are wrong. Or if the constant bounds are right, then you'll save yourself a lot of trouble by just declaring the array with constant dimensions. – John Bollinger Jul 31 '22 at 23:59
  • @JohnBollinger Yes, you are correct with the array having constant dimensions, but I want to use row and col in a way that they function as variables in the loop. How would I initialize the array with still assigning each cell of the array based on the row and col value? – Ryan Paye Aug 01 '22 at 00:03
  • Assigning values to cells based on cell indices has nothing whatever to do with using a variable-length array. In fact, in the code presented, the `row` and `col` variables inside your loops aren't even the same variables as the file-scope variables of the same names, so you already have no connection at all. If indeed you don't need variable array dimensions then start with the simple thing: make the array dimensions constant. – John Bollinger Aug 01 '22 at 00:10
  • @JohnBollinger How do I assign a cell to be [row][col] or some variation that doesn't imply a variable-length array? – Ryan Paye Aug 01 '22 at 00:24
  • 1
    Clearly there is some kind of disconnect or deep misunderstanding here. I'm not sure what that is. What makes you think that the declaration `int myrowcol[col][row];` is doing or should do anything for you that `int myrowcol[7][7];` would not? – John Bollinger Aug 01 '22 at 00:32

0 Answers0