char testStr[] = " trim this ";
char** pTestStr = &testStr;
trim(pTestStr);
int trim(char** pStr)
{
char* str = *pStr;
while(isspace(*str)) {
(*pStr)++;
str++;
}
if(*str == 0) {
return 0;
}
char *end = str + strlen(str) - 1;
while(end > str && isspace(*end))
end--;
*(end+1) = 0;
return 0;
}
最佳答案
你需要让 testStr
可写:
char testStr[] = " trim this ";
问题是 char *ptr = ...
有 ptr
指向只读内存中的实际文字字符串。
通过使用 char testStr[] = ...
,您正在分配一个数组并使用与文字字符串相同的内容初始化该数组。由于这是一个数组,因此它是可写的。
关于c - C 中的 Trim 函数,用于原地修剪(不返回字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3019490/