Quando minha função is_ingredient_list
passa um ponteiro para outra função is_ingredient
, que copia o conteúdo para outro array e o tokeniza, meu ponteiro original é afetado e para de tokenizar após o primeiro token. Como consertar esse problema?
#include <stdio.h>
#include <string.h>
int is_ingredient(const char* str){
char tmp[1024];
strncpy(tmp, str, sizeof(tmp));
tmp[sizeof(tmp)-1] = '\0';
char delim[] = " ";
char *token = strtok(tmp, delim); // this line causes the problem
//printf("%s\n", str);
return 1;
}
int is_ingredient_list(const char* str){
char tmp[1024];
char delim[] =",";
strncpy(tmp, str, sizeof(tmp));
tmp[sizeof(tmp)-1]='\0';
char* token = strtok(tmp, delim);
while(token!=NULL){
if(is_ingredient(token)==0) return 0;
printf("%s\n", token);
token = strtok(NULL, delim);
}
return 1;
}