Julie said:
Julie said:
This doesn't allocate or move memory, but operates on the source string:
<see previous post>
Bug in TrimRight, should be:
char * TrimRight(char * string)
{
char * end = (string && *string) ? &string[strlen(string)-1] : 0;
while (end && end>=string && isspace(*end))
{
*end = '\0';
--end;
}
return string;
}
I don't think your "end >= string" check is portable. IIRC,
less-than/greater-than/etc pointer comparisons are only valid if the pointer
points to something inside the block of memory or one beyond the last
element. If you move the pointer to just before the first element, you
can't expect the comparison to work on all platforms. In practice, it
generally works, though.
For a quick fix, you could remove the >= test and instead insert a break
when end == string, or count the characters overwritten, comparing it
against what strlen() returned so you know when to stop.