Array with size from user input -


this question has answer here:

i have bit of code:

fmt.scanf("%dx%d", &sizex, &sizey)   var grid [sizex][sizey]int 

i'm getting "non-constant array bound sizex" error. how can create array dimensions chosen user? of course, don't want modify size later, can't use constants.

i think you're looking for;

grid := make([][]int, sizex) := 0; < len(grid) i++ {      grid[i] = make([]int, sizey) } 

this sort of using new keyword in c++ verse regular "on stack" allocation. use declaration in question size args have values known @ compile time.

in example above make applies first dimension, giving me array of []int arrays, however, none of arrays have been initialized have iterate on grid , call make sizey second dimension allocated.


Comments