I'm new to c and I was trying to do simple stuff to learn about strings and char arrays, I'm stuck in this section of code.
const char *words(int count) {
char *words = "words";
if(count==1) {
words[strlen(words)-1] = '\0';// segmentation fault
}
return words;
}
what it does is returning word when count equals one and returns words otherwise. my question is why is this line of code problematic, why I cant look at string as an array of char's
words[strlen(words)-1] = '\0';
I tried to declare the string(array of chars ) in a different way like
char *words = {'w','o','r','d','s'};
but I still get a segmentation fault.