-3

Coming from C, I was always taught that char * and string are very similar to each other. Here is a piece of code that I implemented using string and char * .

The latter gives an error.

prog.cpp: In function 'char* foo()':

prog.cpp:8:15: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] char *bro="karan";

Why does the latter not work as intended?

The string one:

#include<iostream>
#include<string>

using namespace std;

string foo()
{
    string bro="karan";
    return bro;
}

int main()
{
    cout << foo() << endl;
    return 0;
}

The char * one:

#include<iostream>
#include<string>

using namespace std;

char *foo()
{
    char *bro="karan";
    return bro;
}

int main()
{
    cout << foo() << endl;
    return 0;
}
Karan Singh
  • 1,114
  • 1
  • 13
  • 30
  • 2
    "The latter gives an error." And we should just guess this error? – DimChtz Oct 29 '17 at 10:45
  • s/`string *foo()`/`string foo()` – user0042 Oct 29 '17 at 10:45
  • I think you mean that the *first* code snippet gives errors. And those errors have nothing to do with the difference between `char*` and `std::string`. – Some programmer dude Oct 29 '17 at 10:46
  • sorry. I'll edit it asap. – Karan Singh Oct 29 '17 at 10:47
  • @StoryTeller except for the lack of `const`. – Quentin Oct 29 '17 at 10:49
  • @Quentin - Good point. I was tunnel-visioning on the whole `string` -> `string*` thing. Both samples are equally wrong. – StoryTeller - Unslander Monica Oct 29 '17 at 10:50
  • question edited @StoryTeller – Karan Singh Oct 29 '17 at 10:53
  • Warning is not an error. –  Oct 29 '17 at 10:55
  • 1
    @AnuragDaolagajao - The above is a warning only for legacy reasons. Strict compliance should be adhered to. It's not valid C++ code. – StoryTeller - Unslander Monica Oct 29 '17 at 10:59
  • A string literal is really an array of constant characters. The type of e.g. `"karan"` is `const char [6]`. As all arrays it naturally decays to a pointer to its first element, which is of type `const char*`. And you can't simply convert away `const` in C++ without a special language-construct. I suggest you get [a good C++ beginners book or two](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) as it should be explained in it. – Some programmer dude Oct 29 '17 at 11:02
  • @StoryTeller What I meant to point out was the difference between warning and an error. Code with warning will still compile and run whereas a code with error will fail to compile. In his case, the compiled code will run. Compliance and code validity is a difference story though –  Oct 29 '17 at 11:03
  • @AnuragDaolagajao - No, not really. Turning on compliance options will make the above an error. The difference between an "error" and a "warning" when it's a compliance matter is moot. – StoryTeller - Unslander Monica Oct 29 '17 at 11:07

1 Answers1

1
char *bro="karan";

The bro variable is a char*. The string "karan" is a const char[6] data type, and will implicitly convert to const char*, which means "pointer to a constant value"

Your warning comes from the fact that on that line, you try to assign something that points to const data to something that points to non const data.

Here, std::string and char* differs from the fact that std::string has a constructor that takes a const char*, see reference here, allocate a char array internally, and make a copy of the string, while with the code with char* does not copy anything.

HatsuPointerKun
  • 637
  • 1
  • 5
  • 14