char **letters = new char[8][8]; 

x.cpp: In function 'int main()': x.cpp:2: error: cannot convert 'char (*)[8]' to 'char**' in initialization

    3 answers 3

    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 
    • 2
      Firstly, this answer is not the answer to the question: "Why does an error occur ...". Secondly, what does “need” mean? Who needs"? The author of the question wants to create an array char [8][8] . You also create a "jagged" array, which char [8][8] is not at all. - AnT
    • totally agree - AR Hovsepyan

    The 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];