Hi all
say I have this function:
char *trim(char *string)
If I understand it correctly the function's result value has to be allocated, let's say, by malloc. But how is this allocation going to be freed? All I do in my program is this:
string = trim(string)
Thanks in advance
Here is the function
Code:
char *
trim (char *string)
{
char *result = 0;
/* Ignore NULL pointers. */
if (string)
{
char *ptr = string;
/* Skip leading whitespace. */
while (strchr (WHITESPACE_STR, *ptr))
++ptr;
/* Make a copy of the remainder. */
if ((result = (char *) malloc(strlen(ptr)+1)) == NULL)
{
fprintf(stderr,"trim() - Cannot allocate; %s",ptr);
return NULL;
}
memset(result,'\0',strlen(ptr)+1);
strcpy(result,ptr);
/* Move to the last character of the copy. */
for (ptr = result; *ptr; ++ptr)
/* NOWORK */;
--ptr;
/* Remove trailing whitespace. */
for (--ptr; strchr (WHITESPACE_STR, *ptr); --ptr)
*ptr = '\0';
}
return result;
}