【发布时间】:2012-07-19 05:07:38
【问题描述】:
我有一个函数可以将 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]);
}
}
当我从 main 中这样调用它时,它会引发段错误:
char* needle = "foobar";
toLower(needle, strlen(needle));
我确定问题出在这里的作业中:
word[i] = tolower(word[i]);
但我似乎无法找出正确的方法。我尝试将其传递为char**,或传递*(word+i),但都导致相同的问题。
【问题讨论】: