Question

Why would this get a segmentation fautl?

char *c = "dog";
     printf ("char is %c\n", s[0]);    
     s[0]='i';    
     printf ("s = %s\n", s);

output: char is d segmentation fault

Why does it error on the second string? Im just trying to understand it...

Was it helpful?

Solution

char* is a const string, the char in it should be changed liked "s[0]='i'; " changing the string to a char array will be ok.

char s[] = "dog";
printf ("char is %c\n", s[0]);
s[0]='i';
printf ("s = %s\n", s);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top