I have written a small game in C, which has a board-like structure in the gameplay. So, I have used a 2d array of structures which contain many data elements like integer, bools, chars, etc. (let us call this structure cell
). The array size is MxN, both of which are variables whose values can be changed by the user (and are 10 by default). This array named grid
exists primarily in a function called play()
, which handles the progress of the game, and main
is only used as a menu, to call play()
or settings()
, etc. Thus, when I change the values of M and N in settings()
, go back to main
and choose 'play', a new 2d array (grid
) of the required size is created within the function, and is destroyed when I stop playing (when control exits play()
). All this is done without calling malloc()
or free()
.
Now, my question is: how does this work? M and N are (global) variables - their values are not known to the compiler during compilation. So shouldn't the compiler throw an error?
Bonus question: How do I go about converting this code to use correct dynamic allocation practices, using malloc?