Reputation: 181
This might sound pretty old school, but I'm still unable to figure out why the following program throws a segmentation fault. Any help would be great
#include <stdio.h>
pointer(char **x)
{
printf ("Before %c",*x[0]);
*x[0] = 'a'; // segmentation fault here!!
printf ("After %c", *x[0]);
}
int main()
{
char *x = "Hello";
pointer(&x);
}
Upvotes: 1
Views: 73
Reputation: 868
char *x = "Hello";
This declaration makes it read-only. Writing to it the way you tried is illegal.
See this for more information
Upvotes: 1
Reputation: 6437
It's explained in the answer to this question.
TL;DR: the memory pointed to by char *x = "Hello";
is read only. Trying to write to it is illegal, and will result in a segmentation fault.
Upvotes: 1