Home c++ Why does the CANNOT Convert 'Char (*) error occur ' to 'char...

Why does the CANNOT Convert ‘Char (*) error occur [8]’ to ‘char **’ in initialization?

Author

Date

Category

X.CPP: in function 'int main ()':
X.CPP: 2: Error: Cannot Convert 'Char (*) [8]' to 'char **' in initialization


Answer 1, Authority 100%

If I do not change the memory to highlight the memory for a two-dimensional array, you must first initialize the array itself (lines of the array), and then elements of each line.

# Define Rows 8
#Define Cols 8.
char ** letters = new char * [rows];
For (int i = 0; i & lt; rows; i ++) {
  Letters [i] = New Char [cols];
  For (int j = 0; j & lt; cols; j ++) {
    Letters [i] [j] = 'C';
    PrintF ("% C", Letters [i] [J]);
  }
  PrintF ("\ n");
}

Result:

CCCCCCCC
CCCCCCC
CCCCCCC
CCCCCCC
CCCCCCC
CCCCCCC
CCCCCCC
CCCCCCC

Answer 2, Authority 200%

The new operator returns a pointer to the char array [8] , so the initialized variable must be char [8] .

typedef char letters [8];
Letters * Letters = New Char [8] [8];

Answer 3, Authority 200%

Call New Char [8] [8] Creates a two-dimensional array of type char [8] [8] , and, as usual, returns a pointer to its first element: On the char array [8] . That is, the type of result in this case is char (*) [8] . Type Char (*) [8] has nothing to do with your char ** . Therefore, an error occurs.

correctly:

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

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions