0

I got bus error from following code:

char *my_string = "Hello";
my_string[0] = 'Y';

What could be the problem (bug)?

Eziz Durdyyev
  • 1,110
  • 2
  • 16
  • 34
  • 3
    char my_string[]="Hello"; is ok. it copied into the stack memory. but char *my_string="Hello" is pointing to read only memory. so cannot modify. if you want to use heap memory, use malloc and copy the string to the heap. – Junhee Shin Mar 29 '18 at 07:24
  • This is not a duplicate of that specific question. This is about writing to string literals. – marcolz Mar 29 '18 at 07:24
  • It's more a duplicate of https://stackoverflow.com/questions/3303411/string-literals-vs-const-char-in-c – marcolz Mar 29 '18 at 07:35

0 Answers0