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
  • 28,969
  • 9
  • 96
  • 135
user695652
  • 3,915
  • 6
  • 36
  • 54

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
  • 267,863
  • 28
  • 545
  • 906
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
  • 28,969
  • 9
  • 96
  • 135