Re: trim function dumping core



rkk wrote:
Chris Dollin wrote:
rkk wrote:
<snip>
I've written a small trim function to trim away the whitespaces in a
given string.
<snip>
Um. Er. "Small"? Don't you think that this is just a bit big
for left-and-right trimming?

Here's my effort, which I offer as a target ...

#include <stdio.h>
#include <ctype.h>

char *trim( char *s )
{
char *lastNonSpace = 0;
char *current;
while (isspace( *s )) s += 1;
current = s;
while (*current)
{
if (!isspace( *current )) lastNonSpace = current;
current += 1;
}
if (lastNonSpace) lastNonSpace[1] = 0;
return s;
}

int main( int argc, char **argv )
{
char e1[] = "";
char e2[] = "1";
char e3[] = "spoo";
char e4[] = " spoo ";
char e5[] = " spoo for tea! ";
char e6[] = " theLongAndWindingRoad";
fprintf( stderr, "'%s'\n", trim( e1 ) );
fprintf( stderr, "'%s'\n", trim( e2 ) );
fprintf( stderr, "'%s'\n", trim( e3 ) );
fprintf( stderr, "'%s'\n", trim( e4 ) );
fprintf( stderr, "'%s'\n", trim( e5 ) );
fprintf( stderr, "'%s'\n", trim( e6 ) );
return 0;
}
<snip>
This code doesn't work on my machine. Seems to be broken somewhere.

It compiles fine and appears to work well enough here. A couple of
warnings about unused argc and argv are produced.

.



Relevant Pages

  • Re: Which programming method is better?
    ... process("standard input", stdin, flags) ... and not a filename argument. ... preincrement argv first. ... I'd also go with const char* instead of char*, ...
    (comp.lang.c)
  • Re: system() call on linux causes errno set to SIGCHLD
    ... int main(int argc, char* argv[]) ... of the first app succeeds. ...
    (comp.unix.programmer)
  • Re: Understanding char **argv
    ... char **argv looks like. ... So as to dereference what the pointer that *argv pointed to. ... but modifying argv while leaving argc alone is odd. ...
    (comp.lang.c)
  • Re: Weird lappend issue...
    ... string argument passed in through argv. ... int modify(ClientData clientdata, Tcl_Interp* interp, int argc, char* ...
    (comp.lang.tcl)
  • Re: Style vs function? in arrays
    ... argv has type char **, and it's initially pointing at the first element ... When you do ++argv, you're moving it to point to ... the /second/ element in that array of char *. ... pointer incrementation, is there a good reason for this or is this ...
    (comp.lang.c)