No.
myArray is technically an array of two elements, where each element is an array of 200 char elements.
Array elements are stored right next to each other in memory and
all elements need to have the same size. If they had different sizes the array indexing would not be as efficient.
So that is why you need to specify all but the first dimension of the array, so that the size of the elements are known.
There are of course ways around it. You could for example store an array of pointers.
1 2 3
|
char str1[] = "This is a test...";
char str2[] = "How can we det...";
char* myArray[] { str1, str2 };
|
(accessing the elements is done the same way as before)
If you're not going to modify the strings you can store pointers to the string literals directly. If you do this you have to mark the pointers as
const.
|
const char* myArray[] { "This is a test...", "How can we det..." };
|
But in C++ there is a library type that is designed for storing strings that you might want to use. It's called std::string and becomes available if you include the <string> header.
|
std::string myArray[] { "This is a test...", "How can we det..." };
|
(if you use
using namespace std; you don't need the
std:: in front of
string)
At some point you will also learn about std::vector which is an alternative to arrays that has several advantages. It can be resized, it doesn't lose information about its size when being passed to functions, etc. so if I were to write something like this it would probably look more like the following:
|
std::vector<std::string> myStrings { "This is a test...", "How can we det..." };
|