This is one of those questions where there are so many answers, and yet none do the specific thing.
I tried to look at all of these posts — 1 2 3 4 5 6 7 8 9 — and every time the solution would be either using VLAs, using normal arrays with fixed dimensions, or using pointer to pointer.
What I want is to allocate:
So please, if there is a post I skipped, or didn't read thoroughly enough, that fulfils these requirements, point me to it.
Otherwise, I would ask of you to educate me about this and tell me if this is possible, and if so, how to do it.
You can dynamically allocate a contiguous 2D array as
int (*arr)[cols] = malloc( rows * sizeof (int [cols]) );
and then access elements as arr[i][j]
.
If your compiler doesn’t support VLAs, then cols
will have to be a constant expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With