The program is:
#include
#include
int main(void) {
char *a=\"abc\",*ptr;
ptr=a;
ptr++;
*ptr=\'k\';
printf(\"
The problem is because you are trying to change the string literal "abc"
with:
char *a="abc",*ptr;
ptr=a; // ptr points to the 'a'.
ptr++; // now it points to the 'b'.
*ptr='k'; // now you try to change the 'b' to a 'k'.
That's undefined behaviour. The standard explicitly states that you are not permitted to change string literals as per section 6.4.5 String literals
of C99:
It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
It will work if you replace:
char *a="abc",*ptr;
with:
char a[]="abc",*ptr;
since that copies the string literal to a place that's safe to modify.