neo
neo

Reputation: 47

why does putting a null char in string give error in c

im trying to run this code

#include <stdio.h>
#include <string.h>

#if defined(_WIN32)
#define SEPERATOR '\\'
#elif defined(__unix__) || defined(__linux__)
#define SEPERATOR '/'
#endif

int main(int argc, char *argv[]) {
    char *path = "c/c/c/c/c";
    char *end = strrchr(path, SEPERATOR);
    *end = '\0';
    // printf("%s",end);
    printf("%s",path);
    // printf("%c",SEPERATOR);
}

i want to remove the last c from the string. why does this program give segmentation fault. i know it's something to do with memory management. but please shed some light on this.

Upvotes: 0

Views: 35

Answers (0)

Related Questions