Hello,
I am wondering how I could make an array which contains arrays, but with a variable size.
My first try..
But this isn't proper. Any guidance?Code:int array[][] = {{1}, {2}};
Hello,
I am wondering how I could make an array which contains arrays, but with a variable size.
My first try..
But this isn't proper. Any guidance?Code:int array[][] = {{1}, {2}};
You can only leave the left-most [] of a multi-dimensional array empty.
For all the minor sizes, you must specify a size.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
You can use VLAs, but you can't use them with initializers. So you can declare the array, but you can't initialize it:
The other option is dynamic memory allocation: Question 6.16. This also requires you to fill data with loops.Code:int x = 3, y = 4; // feel free to read this in from user/file or generate in some other way int array[x][y]; // can't use an initializer = {{1, 2, 3}, ...}, fill using loops.