5

If i define something like below,

char  *s1 = "Hello";

why I can't do something like below,

*s1 = 'w'; // gives segmentation fault ...why???

What if I do something like below,

string s1 = "hello";

Can I do something like below,

*s1 = 'w'; 
2501
  • 25,460
  • 4
  • 47
  • 87
user882196
  • 1,691
  • 9
  • 24
  • 39
  • possible duplicate of [Why is this C code causing a segmentation fault?](http://stackoverflow.com/questions/1614723/why-is-this-c-code-causing-a-segmentation-fault) – Ben Voigt Jan 03 '12 at 21:38
  • 1
    also http://stackoverflow.com/questions/943191 – Ben Voigt Jan 03 '12 at 21:41
  • Not a duplicate because those questions just ask the first part and don't ask for the difference to the C++ string case. – mmmmmm Jan 04 '12 at 14:58

4 Answers4

10

Because "Hello" creates a const char[]. This decays to a const char* not a char*. In C++ string literals are read-only. You've created a pointer to such a literal and are trying to write to it.

But when you do

string s1 = "hello";

You copy the const char* "hello" into s1. The difference being in the first example s1 points to read-only "hello" and in the second example read-only "hello" is copied into non-const s1, allowing you to access the elements in the copied string to do what you wish with them.

If you want to do the same with a char* you need to allocate space for char data and copy hello into it

char hello[] = "hello"; // creates a char array big enough to hold "hello"
hello[0] = 'w';           //  writes to the 0th char in the array
Doug T.
  • 64,223
  • 27
  • 138
  • 202
  • 2
    Just for the correctness: the type of "hello" isn't char const* (or const char* if you insist) but char const[6] which decays at its first chance to char const* (or, accepts being assigned to char*). – Dietmar Kühl Jan 03 '12 at 21:44
2

string literals are usually allocated in read-only data segment.

alexm
  • 6,854
  • 20
  • 24
  • 1
    True but it's more helpful to restrain conversation to the scope of the abstract machine that C++ defines. It already goes out of its way to ensure that string literals are treated as constant data; there is no need to talk about physical realities that make this a useful/safe choice, especially as those realities can vary in factual accuracy across the wide range of physical implementations in use around the world and through time. – Lightness Races in Orbit Jan 18 '15 at 13:07
1

Because Hello resides in read only memory. Your signature should actually be

const char* s1 = "Hello";

If you want a mutable buffer then declare s1 as a char[]. std::string overloads operator [], so you can index into it, i.e., s1[index] = 'w'.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
1

Time to confuse matters:

char s0[] = "Hello";
s0[0] = 'w';

This is perfectly valid! Of course, this doesn't answer the original question so here we go: string literals are created in read-only memory. That is, their type is char const[n] where n is the size of the string (including the terminating null character, i.e. n == 6 for the string literal "Hello". But why, oh, why can this type be used to initialize a char const*? The answer is simply backward compatibility, respectively compatibility to [old] C code: by the time const made it into the language, lots of places already initialized char* with string literals. Any decent compiler should warn about this abuse, however.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380