0

I'm trying to strtok two different strings with the same delimeters in one code. I can only get it to function with one string at a time. It doesn't work if i try to do it with both strings.

It seems like ipdel[i] = strtok (ip," ,.-"); and maskedel[j] = strtok (maske," ,.-"); are interfering with eachother.

Why are the two interfering with eachother and how can i fix it?

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stddef.h>

int i;
int j;
int main()
{
    char ip[16]="128.39.75.1";
    char maske[16]="255.255.254.0";
    char *ipdel[4];
    char *maskedel[4];
    char bitvar[4];
    char *pholder;



  printf ("Splitting string \"%s\" into tokens:\n",ip);
    ipdel[i] = strtok (ip," ,.-");
  //maskedel[i] = strtok (maske," ,.-");

    while (ipdel[i] != NULL)
  {


        i=i++;

        ipdel[i] = strtok (NULL, " ,.-");
        //maskedel[i] = strtok (NULL, " ,.-");
  }

    maskedel[j] = strtok (maske," ,.-");
    while (maskedel[j] != NULL)
  { 


        j=j++;

        maskedel[j] = strtok (NULL, " ,.-");

  }

    for (i=0;i<4;i++){
    printf("%s\n", ipdel[i]);

    }

    for (i=0;i<4;i++){
    printf("%s\n", maskedel[i]);

    }
    return 0;
}

Using strok_s solved my problem. Thanx guys.

    ipdel[i] = strtok_s( ip, seps, &next_token1);
  maskedel[i] = strtok_s( maske, seps, &next_token2);


    while ((ipdel != NULL) || (maskedel != NULL))
    {
        // Get next token:
        for(i=1;i<4;i++)
        {
            ipdel[i] = strtok_s(NULL, seps, &next_token1);
                         maskedel[i] = strtok_s(NULL, seps, &next_token2);
        }
                break;
    }
Roffynaut
  • 1
  • 2
  • That's right, `strtok` only handles one string at a time, as the man page would have told you. You could try `strtok_r` or `strtok_s` (depending on compiler) – Weather Vane May 29 '16 at 16:31
  • The [`strtok`](http://en.cppreference.com/w/c/string/byte/strtok) function is not generally reentrant, and you can't use it to tokenize multiple strings simultaneously (it save state internally, e.g. through `static` local variables). You need to use [`strtok_s`](http://en.cppreference.com/w/c/string/byte/strtok) instead. – Some programmer dude May 29 '16 at 16:32
  • 3
    I really meant http://stackoverflow.com/questions/33465800/strtok-when-process-two-strings-at-same-time – Peter M May 29 '16 at 16:32
  • @PeterM: aargh, the (correct!) answer there hasn't been accepted! – Jongware May 29 '16 at 16:34
  • @RadLexus Yeah .. that annoyed me too – Peter M May 29 '16 at 17:31
  • `strtok()` is not thread-safe, it means in order to work it keeps internal variables that limit its functionality to handling only one task at a time. You cannot use `strtok()` on two strings simultaneously. Use [`strtok_r()`](http://linux.die.net/man/3/strtok_r) instead. – Havenard May 29 '16 at 17:31
  • 1
    Note `strtok_s` is non-portable. All `_s` functions are options for inclusion by compilers in C11 (many don't). If you need portability beyond windoze, you will want to look for another solution. – David C. Rankin May 29 '16 at 17:35

0 Answers0