Possible Duplicate:
Is it possible to modify a string of char in C?轉換的char *爲小寫拋出段錯誤錯誤
我有一個函數來轉換一個char *爲小寫。這是函數:
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]);
}
}
當我這樣稱呼它從主,它拋出一個segfault錯誤:
char* needle = "foobar";
toLower(needle, strlen(needle));
我敢肯定的問題是分配在這裏:
word[i] = tolower(word[i]);
但我似乎無法弄清楚這樣做的核心方法。我嘗試通過它作爲char**
,或做*(word+i)
,但都導致相同的問題。