如何将功能从动态分配更改为使用与strtok相同逻辑的tokinize函数的静态分配。它使用动态分配工作正常,但无法弄清楚我如何修改它以使用静态分配
char **tokenize ( char *string, char delimiter) {
char **lines = NULL;
char **temp = NULL;
char limit[2] = "";
size_t skip = 0;
size_t span = 0;
size_t extent = 0;
size_t line = 0;
size_t len = strlen ( string);
limit[0] = delimiter;
if ( NULL == ( lines = malloc ( sizeof ( *lines) * 2))) {//allocate two pointers
fprintf ( stderr, "malloc problem\n");
return NULL;
}
lines[line + 1] = NULL;//sentinel
while ( extent < len) {
skip = strspn ( string + extent, limit);//get number of delimiters
extent += skip;//advance past spaces
if ( ( span = strcspn ( string + extent, limit))) {//find next delimiter or '\0'
if ( NULL == ( lines[line] = malloc ( span + 1))) {
fprintf ( stderr, "malloc problem\n");
return lines;
}
strncpy ( lines[line], string + extent, span);
lines[line][span] = '\0';
if ( NULL == ( temp = realloc ( lines, sizeof ( *lines) * ( line + 3)))) {
fprintf ( stderr, "realloc problem\n");
return lines;
}
lines = temp;
line++;
lines[line] = NULL;
lines[line + 1] = NULL;//sentinel
extent += span;//advance past the token
}
}//loop to end of string
return lines;
}