c++ - How to convert from 1D array to 2D array using malloc -
i'm trying convert 1d array 2d array
if want allocate actual 2d array, not array of pointers, syntax gets little tricky:
int x = 16, y = 8; char (*pt)[x][y] = malloc(x*y);
above, pt
pointer x
y
array of char
s. because it's pointer, accessing elements requires asterisk , parentheses, too:
for (int = 0 ; != x ; i++) { (int j = 0 ; j != y ; j++) { (*pt)[i][j] = (char)(i*x+j); } }
of course need free pointer once done using array:
free(pt);
Comments
Post a Comment