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)
,但都导致相同的问题。