I'm trying to use strstr() to find a substring in a string. Using char[] works only, char* is not working, gives segmentation fault.
So this one is working:
int main() {
    char str[] = "apple apple peach";
    char *p;
    p = strstr(str, "peach");
    if (p!= NULL) {
        strncpy(p, "apple", 5);
    }
    return 0;
}
But this one is not working:
int main() {
    char *str = "apple apple peach";
    char *p;
    p = strstr(str, "peach");
    if (p!= NULL) {
        strncpy(p, "apple", 5);
    }
    return 0;
}
This one is neither:
int main() {
    char *str = "apple apple peach";
    char *peach = "peach";
    char *p;
    p = strstr(str, peach);
    if (p!= NULL) {
        strncpy(p, "apple", 5);
    }
    return 0;
}
And neither this one:
int main() {
    char *str = "apple apple peach";
    char peach[] = "peach";
    char *p;
    p = strstr(str, peach);
    if (p!= NULL) {
        strncpy(p, "apple", 5);
    }
    return 0;
}
Is that a known bug or feature?
 
     
     
     
    