I am trying to run this source code but i am getting the compiler error Expression Must Have a Constant Value. I tried making both the variables x and y constants and assigned them to a significantly big number but then i am getting the error expression must be a modifiable lvalue.I have made comments in the code in front where Visual Studio is giving me the syntax error (red squiggly line), any help !!!!!!
atoi takes a char and you are passing a string. however, if you are going to be using c++ i suggest std::string and std::cin as opposed to char[] and fgets(..., stdin)
In line 31, you are trying to allocate memory statically, or on the stack. Lines 10 - 16 do the same thing. In each of these earlier cases, the compiler can allocate memory at compile time on the program stack to handle the variables requested. In lines 10 - 14, 4 bytes (probably) are set aside for these variables. In line 15 1 byte is set asside, and in line 16 99 bytes are set aside. When the compiler gets to line 31, however, it does not know what the values of x and y are (because the program hasn't been run yet), so it has no idea how much memory to set aside for this 2-dimensional array.
The answer is to allocate memory on the heap, or dynamically. See the following tutorial page.
I tried making both the variables x and y constants and assigned them to a significantly big number but then i am getting the error expression must be a modifiable lvalue.
Another alternative that you apparently tried to do is to declare a "big enough" array statically and then use part of it. The problem you ran into is that you tried to store new values in the constant "variables" used to initialize the array. The constant values you use to initialize the array must be different from the variables you use to read in the user's sizes. What you could have done is something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
constint maxRows = 1000;
constint maxCols = 1000;
int x;
int y;
...
char maze[maxRows][maxCols];
fgets(temp, 99, stdin);
y = atoi(temp);
// Verify that y < maxRows
fgets(temp, 99, stdin);
x = atoi(temp);
// Verify that x < maxCols
...