You can't modify the const pointer after construction. You need a constructor initializer to initialize 'str'.
struct Foo
{
const char* const str;
explicit Foo(const char* str_) : str(str_) {}
};
Note that 'typedef' is not required in C++ for a case like this.
EDIT:
If you can't use a constructor, then you must make your pointer non-const:
struct Foo
{
const char* str;
};
ANOTHER EDIT:
After litb's comment, I tried the original code and it does indeed compile (g++ 4.1.2 and XL C/C++ 8.0) with no warnings. Perhaps the compiler in question is doing a construction followed by assignment in this case? I used less violent strings, but I doubt that would make a difference. ;v)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…