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 chars. 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

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -