I have the code below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** my_spliter(char* text){
char* token;
char** words;
int c_words = 0;
words = (char*) malloc(sizeof(char));
token = strtok(text, ".");
while( token != NULL ) {
if(strlen(token)>1){
words[c_words] = (char*) malloc(strlen(token)*sizeof(char));
strcpy(words[c_words], token);
c_words++;
}
token = strtok(NULL, ".");
}
for(int i=0; i<c_words; i++)
printf("'%s' ", words[i]); //This prints the words successfully
}
void main(){
char *my_text;
char **list;
m_text = (char*) malloc(250*sizeof(char));
strcpy(my_text, ".test..tes.tested...t");
list = my_spliter(my_text);
printf("%s\n", list[0]); //This gives me an error
size_t i;
for (i = 0; list[i] != NULL; i++){
printf("%s\n", list[i]); //This also gives me an error
}
}
I can print the list inside the my_spliter function as mentioned in the comment, but I can't print it outside of it (in main function).
Generally, I want to know how to print 2D dynamic array returned from a function.