Possible Duplicate:
Is it possible to modify a string of char in C?
I have a function to convert a char* to lower case. This is the function:
void toLower(char* word, int length)
{
int i;
for(i = 0; i < length; i++) {
// this I can do
// printf("%c", tolower(word[i]));
// this throws segfault
word[i] = tolower(word[i]);
}
}
When I call it like this from main, it throws a segfault error:
char* needle = "foobar";
toLower(needle, strlen(needle));
I'm certain the issue is in the assignment here:
word[i] = tolower(word[i]);
But I can't seem to figure out the corect way to do it. I tried passing it as char**
, or doing *(word+i)
, but all cause the same issue.