Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ initialization char array by pointer [duplicate]

Tags:

c++

pointers

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

like image 989
Jozko Avatar asked Sep 21 '26 23:09

Jozko


1 Answers

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.

like image 175
Matthieu Brucher Avatar answered Sep 24 '26 00:09

Matthieu Brucher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!