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
  • 119,891
  • 44
  • 235
  • 294

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
  • 119,891
  • 44
  • 235
  • 294
0

The fault is

*ptr = 'T';

It should be a char array,not a string.

Paul Draper
  • 78,542
  • 46
  • 206
  • 285