There's a lot wrong in your code.
char** a = char[255][255]; // error: type name is not allowed
First of all this is not even valid C++ (or C for that matter). Maybe you meant:
char a[255][255];
In any case always remember that the type of a bi-dimensional dynamically allocated array is not **
but (*)[N]
which is very different.
char** a = new char[255][255]; // error: a value of type "char (*)[255]" cannot be used to initialize an entity of type "char **"
The error message you provide in the comment explains exactly what I said earlier.
char a[][] = {"banana", "apple"};
In the above code the correct type of the variable a
should be char* a[]
. Again, arrays and pointer (for what the type is concerned) are very different things. A char
array may decay to pointer (if NULL
terminated), but for the rest, except with explicit casts, you can't use pointers and arrays like you are doing.
The last one worked because, like I said earlier, char* []
is the correct type for an array of C-strings.
Anyway, if you just doing homework, it is ok to learn this things. But in future development using C++: try not to use "features" that start with C-
, like C-strings, C-arrays, etc. C++'s standard library gives you std::string
, std::array
, std::vector
and such for free.
If you really need to allocate dynamic memory (with new
and delete
, or new[]
and delete[]
) please use smart pointers, like std::shared_ptr
or std::unique_ptr
.
char **a = char[255][255]
is simply not valid C++, so it won't compile.