I'm working in C++17 project, but have to use a C-legacy-library. For this I have to create a const char* array in C-style, but im struggling with the initialization. In particular,
#include <iostream>
int main(int argc, char* argv[])
{
const int bound = 3;
const char* inames[bound]{ "" };
for(unsigned int i = 0; i < bound; i++) {
const char *number = std::to_string(i).c_str();
inames[i] = number;
}
for(unsigned int i = 0; i < bound; i++) {
std::cout << "inames["
<< std::to_string(i)
<< "] = "
<< inames[i]
<< std::endl;
}
return 0;
}
returns
inames[0] = 2
inames[1] = 2
inames[2] = 2
as output, which I don't unterstand. I expected the output to be
inames[0] = 0
inames[1] = 1
inames[2] = 2
Can anyone please help me point me to my error?