Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A value of type "const char*" cannot be used to initialize an entity of type "char *"

Tags:

c++

I have a code like this but I keep receiving this error :

A value of type "const char*" cannot be used to initialize an entity of type "char *" 

What is going on?
I have read up on the following threads but have not been able to see any result to my answer as all of them are either from char to char* or char* to char:
Value type const char cannot be used to initialize an entity of type char*
Value of type char* cannot be used to initialize an entity of type "char"

#include <iostream>; using namespace std;  int main() {     int x = 0; //variable x created     int cars (14);//cars is created as a variable with value 14     int debt{ -1000 };//debt created with value 1000     float cash = 2.32;     double credit = 32.32;     char a = 'a';//for char you must use a single quote and not double     char* sandwich = "ham";     return 0; } 

I am using Visual Studio Community 2017

like image 461
nTIAO Avatar asked Feb 13 '18 04:02

nTIAO


People also ask

What does const char * means?

const char *ptr : This is a pointer to a constant character. You cannot change the value pointed by ptr, but you can change the pointer itself. “const char *” is a (non-const) pointer to a const char.

What is the use of const char * in C?

const char* function (...) returns a pointer to data that cannot change. The caller can see the data but the compiler will reject any code that may change the data.

Can a char * be passed as const * argument?

Yes, it's acceptable. The compiler will call the string(const char *) constructor and create a temporary that will be bound to s for the duration of the call.

Can const char * Be Changed?

const char* const says that the pointer can point to a constant char and value of int pointed by this pointer cannot be changed.


2 Answers

That is correct. Let’s say you had the following code:

const char hello[] = "hello, world!"; char* jello = hello; // Not allowed, because: jello[0] = 'J'; // Undefined behavior! 

Whoops! A const char* is a non-const pointer to const char. If you assign its value to a non-const char*, you’ve lost its const property.

A const pointer to non-const char would be a char* const, and you can initialize a char* from that all day if you want.

You can, if you really want, achieve this with const_cast<char*>(p), and I occasionally have, but it’s usually a sign of a serious design flaw. If you actually get the compiler to emit instructions to write to the memory aliased by a string constant, you get undefined behavior. One of the many things that might go wrong is that some implementations will store the constant in read-only memory and crash. Or the same bytes of memory might be re-used for more than one purpose, because after all, we warned you never to change it.

By the way, the rules in C are different. This is solely for backward-compatibility with early versions of C that did not have the const keyword, and you should never write new code that uses a non-const alias to a string constant.

like image 94
Davislor Avatar answered Sep 19 '22 15:09

Davislor


You need to make your string literal type const because in C++ it is a constant array of char, unlike C where it is just an array of char. You cannot change a string literal, so making it const is preferred in C++ for extra safety. It is the same reason you have to use an explicit cast when going from const char* to char*. It's still technically "allowed" in C++ since it is allowed in C, which is why it's just a warning. It's still bad practice to do so. To fix the warning, make it const.

const char* sandwich = "ham"; 
like image 39
Justin Randall Avatar answered Sep 16 '22 15:09

Justin Randall