1

I am using the following code

#include <iostream> 
using namespace std;
int main()
{
    char* Our_String;
    Our_String = "This is a string";
    cout << Our_String;
    return 0;
}

and getting the error as:

a value of type const char * cannot be assigned to an entity of type char *

'=': cannot convert from 'const char[17] to 'char *'

What am I doing wrong.

3 Answers3

1

This conversion of a string literal to char * is deprecated. Your compiler might compile this code (and issue a warning) or not compile it at all.

Suppose that this was accepted. What would happen if you tried to change one of the characters in Our_String after the assignment? You can't change the string literal since it is stored in the binary as a constant. You can even see this. Change to const char* and compile the code. After that, open the compiled executable in a text editor and you should be able to find the "This is a string" string with a simple text search.

darcamo
  • 3,294
  • 1
  • 16
  • 27
  • 1
    This "A string literal is an array of const char*" is a wrong statement.:) – Vlad from Moscow Sep 22 '20 at 19:11
  • It is not an array of pointers. It is an array of ```char const``` (actual characters, not pointers). Arrays can decay into a pointer to the first element, which in this case, is a "char const", so the array->ptr conversion yields a ```char const *``` – Chris Uzdavinis Sep 22 '20 at 19:17
  • That is true, I was still editing the answer. I think it is more clear now, without needless touching the "decay into a pointer" subject. Thanks for the inputs. – darcamo Sep 22 '20 at 19:21
  • That conversion was deprecated in C++98 and C++03; it was **removed** in C++11. As with any code that doesn't conform to the requirements of the C++ standard, a conforming compiler must issue a diagnostic. Having done that, it is free to compile the code, with an implementation-specific meaning. – Pete Becker Sep 22 '20 at 20:45
1

Opposite to C in C++ string literals have types of constant character arrays. (Though in the both languages you may not change string literals.)

Arrays used in expressions with rare exceptions are converted to pointers to their first elements.

So in this expression statement

Our_String = "This is a string";

the string literal has the type const char [17] and implicitly is converted to the type const char *.

However the left operand of the expression with the assignment operator has the type char * due to this declaration

char* Our_String;

You may not assign a pointer to constant object to a pointer to non-constant object.

Hence you need to rewrite the declaration above like

const char * Our_String;

In this case the both subexpressions of the assignment will have the same type const char *.

Another approach is to declare an array instead of the pointer and initialize it with the string literal as for example

char Our_String[] = "This is a string";

and then you can output it the same way as you are outputting the pointer

cout << Our_String;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0
#include <iostream> 

using namespace std;

int main()
{
    char const* Our_String;
    Our_String = "This is a string";
    cout << Our_String;
    return 0;
}