2

In my course on C++ is used this initialization.

char *a = "abcd";

However, when I use it, a compiler complains:

a value of type "const char*" cannot by used to initialize an entity of type "char*"

*a should be pointer. What is a problem please? I use Visual Studio 2017

Jozko
  • 85
  • 1
  • 11
  • 1
    The error message just can't be clearer: a string literal is of type `const char *`, but you're trying to assign it to a symbol of type `char *`. – ForceBru Nov 18 '18 at 19:14
  • @ForceBru actually, a string literal is of type `const char[N]`. It *decays* to a `const char *` – Remy Lebeau Nov 18 '18 at 21:29

2 Answers2

7

You need to specify const

const char *a = "abcd";

The reason is that the string "abcd" is a constant and thus should not be assigned to a non const pointer. It was tolerated in old C++ (AFAIK), but since C++11, it's not, and VS2017 with /permissive- does the right thing and forbids this bad practice.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
2
 const char *a = "abcd";

you forgot const.

johnathan
  • 2,315
  • 13
  • 20