0
char * str = "Hello";

*(str+1) = '3';

cout<<str;

What I was trying to do there was to change the second character into '3', turning it into H3llo

Why doesn't it work?

Pavan Manjunath
  • 27,404
  • 12
  • 99
  • 125
user1091856
  • 3,032
  • 6
  • 31
  • 42

6 Answers6

3

This is undefined behaviour. You cannot change literal.

To have a pointer to literal, it should be:

  const char* str = "Hello";
//^^^^^

Then, to be able to change string, this should be, for example

char str[] = "Hello";

The other option is to allocate dynamically the memory (using malloc and free)

Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • So an error occurred because the compiler caught it or because the program somehow crashed when I tried to rewrite in that memory location? – user1091856 Aug 13 '12 at 07:21
  • @user1091856 - If the compiler have caught it, then there will be a compiler error. If the program crash, it will immediately exit. But it's not necessary to crash. You are not allowed to change that memory and if you try to change it, anything could happen. That's why it's _undefined behaviour_. – Kiril Kirov Aug 13 '12 at 07:23
2

string literals are allocated in read only memory.so basically they are of type(const char *).It cannot be changed. Also see this for more information.

Community
  • 1
  • 1
Vijay
  • 65,327
  • 90
  • 227
  • 319
1

Because str is of type "const char *" and you are not allowed to overwrite the object it points to.

Yang
  • 777
  • 1
  • 10
  • 19
0
#include <string.h>
char *str;
if((str = malloc(strlen("hello"))) != NULL)
  return (null);
str = strcpy(str, "hello");
printf("%s\n", str); // should print hello
str[2] = '3';
printf("%s\n", str) // should print he3lo

The thing here is that i allocate memory before to set char in the string. But if you're not good with allocation you can always set the char str[] = "hello";

C404
  • 243
  • 4
  • 14
  • 1
    The thing is, why use heap allocation when you don't need to? One of the things it leads to is that missing `free` call. – chris Aug 13 '12 at 07:31
  • True to this. Still, its a good way to see the different options – C404 Aug 13 '12 at 07:59
0

Memory for str will be allocated in .rodata section. so trying to modify read only data will produce problem.

The following problem gives issue.

#include <stdio.h>

int main()
{
char * str = "Hello";

printf("\n%s \n", str);
*(str+1) = '3';
printf("\n%s \n", str);


return 0;
}

corresponding dis-assembly

 .file   "dfd.c"
        .section        .rodata
.LC0:
        .string "Hello"
.LC1:
        .string "\n%s \n"
        .text
  .....
  .....

And the result is

Hello 
Segmentation fault (core dumped)

Im using gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) on X86_64.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
0

str is a pointer to string constant and memory for the string is allocated in read-only section . If u try to modify the string content the result is undefined. However you can modify the pointer to point something else as compared to an array-name which is always bound to same memory location.

Manish
  • 1,999
  • 2
  • 15
  • 26