Q: I know, in both cases 'a' represent character pointer.
No
Because when you declare as char *a = "hello";
then a
is pointer of constant string literal and by doing a[i] = 'A';
you are tring to write on read only memory that is illegal.
Whereas in first declaration char a[] = "hello";
, a
is any array and its contents initialized by string "hello"
that array a[]
can be modify latterly in your code. and doing a[i] = 'A';
is perfectly correct.
Q: First compiles successfully but second gives segmentation fault.
Your code compiled because syntax-wise a[i] = 'A';
is correct. But in first case (char *a = "hello";
) instruction
a[1] = 'c';
modifying on constant string literal that is illegal memory operator, this instruction causes invalid action on a valid memory location that can be detected by runtime environment and send a termination signal SIGSEGV which results your program terminated with a segmentation-fault.