Skip navigation.
 
mlRe: C strtok question
FROM : János
DATE : Thu Apr 05 20:49:38 2007

On Apr 5, 2007, at 2:03 PM, Peter Ilberg wrote:

> In your test case, strtok is returning NULL on its last iteration 
> through
> the loop. strstr doesn't like NULL as an input argument. You avoid the
> problem in your longer test case by explicity jumping out of the loop
> after processing "-m". Try rewriting the loop this way:
>
>      flg = (char *)cflags;
>      tokenval = (char *)delim;
>      for (token = strtok(flg,delim); token; token = strtok
> (NULL,delim)) {
>          if ( strstr( token,"-t") != NULL)    {
>              tokenval = strtok(NULL,delim);
>              code = tokenval;
>          }
>          [...other options...]
>          if ( strstr( token,"-m") != NULL)    {
>              mr = 1;
>          }
>          fprintf(stderr, "token=%s  tokenval=%s\n", token,tokenval);
>      }
>
> If all of your options are single letters, you can rewrite the loop 
> body:
>
>          if (token[0] == '-')
>              switch (token[1]) {
>              case 't':
>                  tokenval = strtok(NULL,delim);
>                  code = tokenval;
>                  break;
>              [...other options...]
>              case 'm':
>                  mr = 1;
>                  break;
>              }
>
> --- Peter
> _______________________________________________


Peter,

That is it.

/I found out in the meantime as I tried to debug/

Thanks a lot,

János
P.S.  Almost all C books I read say that I should use switch just 
with int values, so I was afraid to do so although originally that 
was my first choice._______________________________________________
MacOSX-dev mailing list
<email_removed>
http://www.omnigroup.com/mailman/listinfo/macosx-dev

Related mailsAuthorDate
mlC strtok question János Apr 5, 19:10
mlRe: C strtok question Norman Gray Apr 5, 19:23
mlRe: C strtok question Peter Ilberg Apr 5, 20:03
mlRe: C strtok question János Apr 5, 20:49
mlRe: C strtok question Andy Armstrong Apr 5, 20:57
mlRe: C strtok question Uli Kusterer Apr 6, 12:26
mlRe: C strtok question Koryn Grant Apr 11, 08:35
mlRe: C strtok question Uli Kusterer Apr 11, 11:27
mlRe: C strtok question George Warner Apr 11, 18:10
mlRe: C strtok question Graham J Lee Apr 11, 18:40
mlRe: C strtok question János Apr 11, 18:57
mlRe: C strtok question Koryn Grant Apr 11, 21:36