The following code produces a segmentation fault on my system. I can't figure out why. Any help would be appreciated.
#include<stdio.h>
int main() {
char * a = "abc";
*a = 'c';
printf("%c\n", *a);
return 0;
}
The following code produces a segmentation fault on my system. I can't figure out why. Any help would be appreciated.
#include<stdio.h>
int main() {
char * a = "abc";
*a = 'c';
printf("%c\n", *a);
return 0;
}
The standard explicitly lists this as undefined behavior in §J.2:
If you want to copy it into a local array, do:
a
is an array on the stack, and you can modify it freely.Attempting to modify a string literal causes undefined behaviour.