Character Pointer And Character Array Problem

Can Somebody explain why this code
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>

int main(){

    char *p="hello";
(*p)++;
    printf("%s",p);


return 0;
}


crashes at runtime . but this code
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>

int main(){

    char p[]="hello";
(*p)++;
    printf("%s",p);


return 0;
}

works perfectly fine
The difference is that in the first one, p is a pointer to a constant string. Your program crashes because you're running it in an environment that detects writes into this read only memory. Even if the program didn't crash, it would still be wrong.

In the second one, p is declared as array of 6 characters on the stack. The content is 'h', 'e', 'l', 'l', 'o', NULL. You are free to change any characters in this range of 6 bytes.
Topic archived. No new replies allowed.