char **letters = new char[8][8];
x.cpp: In function 'int main()': x.cpp:2: error: cannot convert 'char (*)[8]' to 'char**' in initialization
char **letters = new char[8][8];
x.cpp: In function 'int main()': x.cpp:2: error: cannot convert 'char (*)[8]' to 'char**' in initialization
If my memory serves me, in order to allocate memory for a two-dimensional array, you must first initialize the array itself (array lines), and then the elements of each row.
#define ROWS 8 #define COLS 8 char **letters = new char*[ROWS]; for(int i=0; i<ROWS; i++) { letters[i] = new char[COLS]; for(int j=0; j<COLS; j++) { letters[i][j] = 'c'; printf("%c", letters[i][j]); } printf("\n"); }
Result:
cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc
char [8][8]
. You also create a "jagged" array, which char [8][8]
is not at all. - AnTThe new operator returns a pointer to the array char[8]
, therefore, the initialized variable must be char[8]
.
typedef char LETTERS[8]; LETTERS *letters = new char[8][8];
Calling new char[8][8]
creates a two-dimensional array of type char[8][8]
, and, as usual, returns a pointer to its first element: to char[8]
. That is, the type of result in this case is char (*)[8]
. The char (*)[8]
has nothing to do with your char **
. Therefore, an error occurs.
Right:
char (*letters)[8] = new char[8][8];
Source: https://ru.stackoverflow.com/questions/11551/
All Articles