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.

9
  • 2
    Missing const. As the compiler already told you. Commented Sep 22, 2020 at 19:00
  • 2
    Use char const* Our_String; Commented Sep 22, 2020 at 19:02
  • "const char*" and "char const*" both are working fine Commented Sep 22, 2020 at 19:05
  • 3
    "What am I doing wrong." Not using std::string. Commented Sep 22, 2020 at 19:10
  • 1
    Trying to write C in C++. Commented Sep 22, 2020 at 19:15

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This "A string literal is an array of const char*" is a wrong statement.:)
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 *
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.
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.
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;

Comments

0
#include <iostream> 

using namespace std;

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

1 Comment

An answer consisting only of code is considered incomplete. Please explain the changes made and what problem those changes overcome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.