Critique my string splitting API
Hello,
This is probably petty, if not very petty but...
Can someone critique my string splitting API.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static char** splitstring(char *str, int *noOfTokens)
{
char **result = malloc(sizeof(char));
char *token = strtok(str, " ");
*noOfTokens = 0;
while (token)
{
result[*noOfTokens] = malloc(strlen(token));
strncpy(result[*noOfTokens], token, strlen(token));
token = strtok(NULL, " ");
(*noOfTokens)++;
}
return result;
}
int main(int argc, char *argv[])
{
int noOfTokens;
char tokenize[] = "This is a string\0";
char ** tokens = splitstring(tokenize, &noOfTokens);
for (int i = 0; i < noOfTokens; i++)
{
printf("%s\n", tokens[i]);
}
return 0;
}