Reputation: 1
I would like to have a dynamic 2D array of chars, max 200 and scan to it. I am not really sure how to do this, all I came up with is to define the dynamic arrays (and I don't even know if it is right):
char **arr = (char **)malloc( (len+1) * sizeof(char*) );
for (i=0; (len+1)>0; i++)
char *arr1 = (char *) malloc ( (len+1) * sizeof(char) );
But I am not sure what to put in the (len) - let's say I would like an array of 51x150, depending on the scanned input. How to allocate the array based on the scanned value and print it?
An example of input:
####.#...##
##.##.#.###
##.###.#.##
#.##.##.#..
Upvotes: 0
Views: 562
Reputation: 214300
You probably want to use a real 2D array and not a segmented look-up table.
size_t x = 51; // user input
size_t y = 150; // user input
char (*arr)[y] = malloc( sizeof(char[x][y]) );
...
arr[i][j] = something;
...
free(arr);
This is much faster and safer than a segmented look-up table. This also allows you to memcpy
data in and out of the array, even if that data is larger than one line.
Upvotes: 2