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;
}