I am bit confused about the following C++ code:
#include <iostream>
using namespace std;
void test(const string& str)
{
static const char * const c = str.c_str();
cout << c << endl;
}
int main(int argc, char* argv[])
{
test("Hello");
test("Nooo");
return 0;
}
Since the variable c is declared as static and const, shouldn't this be initialized only once and keep its initial value until the process is completed? According to this reasoning, I was expecting the following output:
Hello
Hello
But I got:
Hello
Nooo
Can you clarify why the value of the variable c has been modified between two function calls even though it is a const variable?