I am curious how to allocate memory for char** when I do not know how many words I will have and what is the maximum length of words. I need to divide string (which I get as char*) on multiple tokens (words) and save separate words in char**. I understand that I can allocate size for *char as (string length+1) and for each char[i] I can allocate also (string length + 1) <- in case we have the whole sting as a one token or we have each character in the string as a separate words. So I think the code would be:
char **words = malloc((strlen(str)+1) * sizeof(char*));
int i;
for( i=0; i < strlen(str)+1; i++)
words[i] = malloc((strlen(str)+1) * sizeof(char));
Is it correct? Can I do if more efficient?