How do you make a multidimensional array of chars on the free store and how do you modifiy the values of each of the chars in the array?
Printable View
How do you make a multidimensional array of chars on the free store and how do you modifiy the values of each of the chars in the array?
Lookup new and delete.
Post what you're tried if you get stuck.
When I try and create a multidimensional array of chars on the free store I do it like this:
This is excactly the way my book does it, only it's a one dimensional array in the examples. With the above I get the error 'cannot convert "char (*)[6]" to "char *". I have no idea what that means.Code:char * board = new char[5][6];
How about:
Code:#include <iostream>
#include <iomanip>
int main(void)
{
const int ROW = 5;
const int COL = 6;
int **array = new int*[ROW];
for (int i = 0; i < ROW; i++)
array[i] = new int[COL];
for (int i = 0; i < ROW; i++)
for (int j = 0; j < COL; j++)
array[i][j] = i*10+j;
for (int i = 0; i < ROW; i++)
for (int j = 0; j < COL; j++)
std::cout <<"array["<<i<<"]["<<j<<"] = "
<<std::setw(2)<<std::setfill('0')
<<array[i][j] <<std::endl;
for (int i = 0; i < ROW; i++)
delete [] array[i];
delete [] array;
return(0);
}
/*
Output
array[0][0] = 00
array[0][1] = 01
array[0][2] = 02
array[0][3] = 03
array[0][4] = 04
array[0][5] = 05
array[1][0] = 10
array[1][1] = 11
array[1][2] = 12
array[1][3] = 13
array[1][4] = 14
array[1][5] = 15
array[2][0] = 20
array[2][1] = 21
array[2][2] = 22
array[2][3] = 23
array[2][4] = 24
array[2][5] = 25
array[3][0] = 30
array[3][1] = 31
array[3][2] = 32
array[3][3] = 33
array[3][4] = 34
array[3][5] = 35
array[4][0] = 40
array[4][1] = 41
array[4][2] = 42
array[4][3] = 43
array[4][4] = 44
array[4][5] = 45
*/
thanks for your help.
Yeesh...Quote:
Code:array[0][0] = 00
array[0][1] = 01
array[0][2] = 02
array[0][3] = 03
array[0][4] = 04
array[0][5] = 05
array[1][0] = 10
array[1][1] = 11
array[1][2] = 12
array[1][3] = 13
array[1][4] = 14
array[1][5] = 15
array[2][0] = 20
array[2][1] = 21
array[2][2] = 22
array[2][3] = 23
array[2][4] = 24
array[2][5] = 25
array[3][0] = 30
array[3][1] = 31
array[3][2] = 32
array[3][3] = 33
array[3][4] = 34
array[3][5] = 35
array[4][0] = 40
array[4][1] = 41
array[4][2] = 42
array[4][3] = 43
array[4][4] = 44
array[4][5] = 45
Code:for(int a=0;a<6;a++)
{
for(int b=0;b<6;b++)
{
array[a][b] = a*10+b;
}
}
@Magos: Your point being??!!:confused: That's the same code I used :rolleyes:
Ouch! I didn't see the quotes, that it was the output. Thought you set the values using those 36 lines of code. Should've known you knew better :o .
I almost posted something about that... then i noticed the comments...
The others have told you how to accomplish that -- I will tell a bit about how *not* to accomplish this :PQuote:
Originally posted by Mr_Jack
How do you make a multidimensional array of chars on the free store and how do you modifiy the values of each of the chars in the array?
Dynamic arrays tend to be a "problem area" of code, and dynamic arrays OF dynamic arrays are even worse. It's hard to make them exception-safe, it's impossible to use them as return values with 100% safety, etc.
Instead, why not use an STL vector of vectors, or vector of strings?
E.g.:
That is it. One line to allocate all the memory, NO lines needed to deallocate (it happens automatically when myArray leaves scope). And it's exception safe. AND you can return an array from a function with 100% certainty that you will not ever leak memory.Code:// Some constants
const int nRows = 5;
const int nCols = 11;
//Typedefs to save our fingers and improve readability
typedef std::vector<char> cArrayRow;
typedef std::vector<cArrayRow> cArray;
// Make an array initially filled with 'A':
cArray myArray(nRows,cArrayRow(nCols,'A'));
Another variation on the same theme:
Code:#include <iostream>
using namespace std;
void populateArray(int* m, int numOfRows, int numOfColumns);
void displayArray(int* m, int numOfRows, int numOfColumns);
int main()
{
unsigned NUM_OF_ROWS = 0;
unsigned NUM_OF_COLUMNS = 0;
cout << "Enter the number of rows: " << flush;
cin >> NUM_OF_ROWS;
cout << "Enter the number of columns: " << flush;
cin >> NUM_OF_COLUMNS;
int* matrix = new int[NUM_OF_ROWS * NUM_OF_COLUMNS];
populateArray(matrix, NUM_OF_ROWS, NUM_OF_COLUMNS);
displayArray(matrix, NUM_OF_ROWS, NUM_OF_COLUMNS);
delete [] matrix;
return 0;
}
void populateArray(int* m, int numOfRows, int numOfColumns)
{
for(int row=0, value=0; row < numOfRows; row++){
for(int column=0; column < numOfColumns; column++){
m[(row*numOfColumns)+column] = value++;
}
}
}
void displayArray(int* m, int numOfRows, int numOfColumns)
{
for(int row=0; row < numOfRows; row++){
for(int column=0; column < numOfColumns; column++){
cout << m[(row*numOfColumns)+column] << " ";
}
}
endl(cout);
}