Hello guys,
I'm working with a class that contains an 2-D array of characters (char[][]).
In my constructor, I am taking in a string, with length a*b (dimensions of the character array), and assigning them to the array of characters.
For example, call the class StringToChar, which has a private char[3][3] as a class variable. Calling the constructor StringToChar("123456789") should initialize my char[][] as such:
1 2 3
4 5 6
7 8 9
The hard coding of [3][3] is not an issue as everything passed to the class has the same dimensions.
Here is my implementation of the constructor:
1 2 3 4 5 6 7 8
|
const int arrSize = 3;
StringToChar::StringToChar(const std::string& str) {
for (int i = 0; i < arrSize* arrSize; i++) {
charArr[static_cast<int>(i / arrSize)][i % arrSize] = str.at(i);
}
}
|
However, I get the following error in the line inside the for loop
|
charArr[static_cast<int>(i / arrSize)][i % arrSize] = str.at(i);
|
Assigning to 'char **' from incompatible type 'const std::basic_string<char, std::char_traits<char>, std::allocator<char> >::value_type' (aka 'const char')
I had a couple questions about this:
1. The recommended fix in my IDE is to cast this as a char**. It resolves the issue but I do not understand why, and seems to me like just a way to cause more bugs down the line. So what should be the best fix here?
2. Why is a char[][] represented as a char**? Isnt a char** a pointer to a pointer to a char?
Thank you so much for any help!