the*_*ine 2 c string pointers trim
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;
}
Run Code Online (Sandbox Code Playgroud)
您需要使testStr可写:
char testStr[] = " trim this ";
Run Code Online (Sandbox Code Playgroud)
问题是,char *ptr = ...已经ptr指向实际文字字符串是在只读存储器中。
通过使用char testStr[] = ...您正在分配一个数组,并使用与文字字符串相同的内容初始化该数组。由于这是一个数组,因此可写。