0

I am getting segmentation fault error in this code, but don't know why?

#include <stdio.h>

int main(void)
{
    char *ptr = "Linux";
    *ptr = 'T';

    printf("\n [%s] \n", ptr);

    return 0;
}
Yu Hao
  • 115,525
  • 42
  • 225
  • 281

2 Answers2

4

ptr is a pointer that points to a string literal, but you can't modify a string literal, change it to:

char ptr[] = "Linux";
Yu Hao
  • 115,525
  • 42
  • 225
  • 281
0

The fault is

*ptr = 'T';

It should be a char array,not a string.

Paul Draper
  • 71,663
  • 43
  • 186
  • 262