o erro se encontra na função **get_tokens, na parte tk = strtok(str,space);
por que está dando segfault ?
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
int count_tokens(char str[], char space[])
{
char *tk = NULL;
int ct;
tk = strtok(str," ");
while (tk)
{
ct++;
tk = strtok(NULL,space);
}
free(tk);
return ct;
}
char **get_tokens(char str[],char space[],int size)
{
int i;
int str_len = 500;
char *tk = NULL;
char **tokens;
tokens = malloc(size*sizeof(char)); //alocando memoria pro *tokens
tk = strtok(str,space);
for (i = 0 ; i < size ; i++ )
{
tokens[i] = malloc(str_len*sizeof(char));
if (!i) strcpy(tokens[i],tk);
tk = strtok(NULL,space);
puts(tk);
strcpy(tokens[i],tk);
}
return tokens;
}
void main()
{
char s[] = "a b c d e f g";
int count,i;
char **tks;
count = count_tokens(s," ");
tks = get_tokens(s," ",count);
}