3

As I know, if we declare char* in our program then it gives memory from read-only area, so we are not able to change a char at any position in the array.

char *ch = "sitaram";
ch[2] = 'y';

The above code will not run properly, as we are changing read-only memory.

One approach is we can declare our char array as

char ch[] = "sitaram";

and then we can change a value at an index.

Is there any way where I can change a char value at any index in a char*?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
sitaram chhimpa
  • 471
  • 7
  • 12
  • 3
    `char *ch = "sitaram";` only compiles because of a backwards compatibility "feature". The correct type should be `const char *ch`. As the type implies, you cannot modify this string. – François Andrieux Jul 04 '17 at 17:55
  • `char*` is a **pointer**. When you write `char *ch = "sitaram";` it initializes `ch` to **point to** the first element of an array of `char` that holds the string literal `"sitaram"`. That distinction is important; a pointer is not an array, and an array is not a pointer. – Pete Becker Jul 04 '17 at 18:18
  • It is also important to make the distinction that `char*` itself is not what makes the memory read-only. The pointer is simply pointing at a read-only literal value. Make it point at writable memory instead and then modifying works. Changing `ch` to `char[]` instead of `char*` simply causes the literal data to be copied into the `char[]` memory during initialization. – Remy Lebeau Jul 04 '17 at 19:10

4 Answers4

6

Use the modern C++ approach for mutable string values

std::string str{"sitaram"};
str[2] = 'y';

String literals (i.e. values enclosed in "") are by default of type const char[n] (where n is the length of the string literal +1 for the null character) in C++ and because of that they are immutable, any attempt to modify them results in undefined behavior.

Curious
  • 20,870
  • 8
  • 61
  • 146
  • 5
    _String literals (i.e. values enclosed in "") are by default of type const char*_ ... **nope**, they are of type [`const char[n]`](http://en.cppreference.com/w/cpp/language/string_literal) and only _decay_ to `const char*` in a context where a pointer is required. – zett42 Jul 04 '17 at 17:59
  • @zett42 are they of type `const char[n]` or `const char (&)[n]`? I feel like they are the latter? https://wandbox.org/permlink/sQCCemUzweDCDLAM – Curious Jul 04 '17 at 18:06
  • From [cppreference](http://en.cppreference.com/w/cpp/language/string_literal): _1) Narrow multibyte string literal. The type of an unprefixed string literal is const char[]._ – zett42 Jul 04 '17 at 18:09
  • @zett42 hmm I wonder why `decltype` evaluates to a reference to an array then – Curious Jul 04 '17 at 18:11
  • [Fascinating: _String literals are lvalues_](https://stackoverflow.com/a/30293277/7571258) – zett42 Jul 04 '17 at 18:37
4

When you say:

char *ch = "sitaram";

The compiler does the following:

  • it allocates the string "sitaram" at program start (static storage duration). This string can be put into read-only memory.
  • when your program arrives at this line, it allocates the pointer ch, and makes this pointer to point to the statically allocated "sitaram" string.
  • if you do ch[2] = 'y', then you're trying to modify the 3rd character of the statically allocated string. Usually, you get a crash (because it is in read-only memory)

On the other hand, if you do the following:

char ch[] = "sitaram";

When the program hit this line, it allocates memory for the array ch[] (for 8 chars), then copies the string "sitaram" into this memory. If you do ch[2] = 'y', then you modify this allocated memory, which is perfectly fine to do.

If you want to modify a string with char *, it should point to a memory which is modifiable. For example:

char ch[] = "sitaram";
char *xx = ch;
xx[2] = 'y'; // it is the same as ch[2] = 'y';
geza
  • 28,403
  • 6
  • 61
  • 135
2

Using char arrays:

char text[] = "sitaram";
text[3] = 'o';
char * p = &text[0];
p[4] = 'x';
cout << text;
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
-1

its just idea: you can copy whole value of char* ch into char array change value of i symbol and put new string in another ch2 then take address of pointer ch and assign it to pointer ch2... but probably it won't work...