Reputation:
So I am teaching myself C++ and I have a doubt about arrays. I know that if I declare a 2D array like:
char board[8][8];
I create a 2D array with a height and a width of 8. But, I was wondering if there is a way to set those dimensions to variables so the user can input the height and width they want, I tried this in C++ and visual studio wasn't happy.
int rowSize = 0;
int colSize = 0;
cin >> rowSize >> colSize;
char board[rowSize][colSize];
can anyone help me?
Upvotes: 1
Views: 1461
Reputation: 16876
Since this is tagged with c++
, perhaps std::vector
is for you. It's much like an array, but more convenient and well suited for this purpose. You can simply initialize a 2D Array of nested vectors with this initializer call (thanks to Bob__ for this much more simple solution):
int n = 5;
int m = 10;
std::vector<std::vector<char>> board (n, std::vector<char>(m, 'a'));
This creates the board and initializes all fields to 'a'
, per example. You can access and manipulate the data using the same syntax as with C style arrays:
char x = board[4][2];
board[3][3] = 'o';
Last but not least, there are lots of convenient features and functions that allow you to do things like copying it much more easily than with C-style arrays. Check out the documentation of std::string
here.
Upvotes: 1
Reputation: 148890
No you cannot in conformant C++, because the standard does not define Variable Length Arrays (*).
If you do not need consecutive allocation (a true 2D array underlying storage), you can use a vector of vectors. This is generally the most simple and idiomatic way:
std::vector<std::vector<char>>(8, std::vector<char>(8, '\0'));
If you can accept a functionnal type access, you can build a custom container with an underlying 1D vector of size 64 (8*8), and an accessor method returning a reference.
If you want to mimic 2D container with an underlying true 2D array, then you are in trouble. I have tried to build a generic multi-dimensional container (code in Code Review), and realized that you cannot correctly implement random access iterators on non standard containers...
(*) of course, gcc gladly accept VLA in C++ as a documented extension to the language...
Upvotes: 0
Reputation: 927
If you really want to do this you could do the follwing:
int* myPointer = nullptr; // Pointer to int, initialize to nothing.
int sizeOfArray; // Size needed for array
std::cin >> sizeOfArray; // At runtime get the size of the array
myPointer = new int[sizeOfArray]; // Allocate array of specified size and save ptr in a.
for (int i = 0; i < sizeOfArray; i++) {
myPointer[i] = 0; // Initialize all elements to zero.
}
delete[] myPointer; // When done, free memory pointed to by myPointer.
myPointer = nullptr; // Clear a to prevent using invalid memory reference.
Although I would strongly recommend you use a vector instead.
Upvotes: 0
Reputation: 22023
If you want to allocate such matrices, use a vector and index it with i + j * rowSize
.
The other construction is not c++ compliant, it's an gcc extension from C99 (Variable Length Arrays), and their equivalent (more or less) are a vector or a unique pointer to an array.
Upvotes: 0