1

How do I initialize a c-string in C++11?

I tried:

char* foo = {"bar"};

but I get:

ISO C++11 does not allow conversion from string literal to 'char *
Niall
  • 30,036
  • 10
  • 99
  • 142
user695652
  • 4,105
  • 7
  • 40
  • 58

2 Answers2

3

Because it has to be pointer to const:

const char* foo = "bar";

Before C++11, you could make foo simply char*, even though that was deprecated since C++03. But C++11 removes the deprecation and simply makes it illegal.

Barry
  • 286,269
  • 29
  • 621
  • 977
2

The correct way to do it is;

char const* foo = {"bar"};
//   ^^^^^ added const

The older C style (non const) was deprecated and now is removed from C++11.

Niall
  • 30,036
  • 10
  • 99
  • 142