I need to solve this exercise this way because the given function needs to have these parameters, the problem is I'm getting core dumped at around the 20th string I read:
int load(char *filename, char ***words) {
char **pt = (*words);
*words = (char **)malloc(sizeof(char*));
char s[50];
FILE *f = fopen(filename, "r");
int i = 0, t;
while (fscanf(f, "%s", s) == 1) {
t = strlen(s);
printf("%d ", t);
s[t] = '\0';
pt = (char **)realloc(pt, sizeof(char*) * (i + 1));
pt[i] = (char *)realloc(pt[i], sizeof(char) * (t + 1));
strncpy(pt[i], s, t + 1);
printf("%s\n", pt[i]);
i++;
}
return 0;
}
I'm using pt as a char**
just to test how this works, the objective is to store in the ***words
the strings in the textfile:
textfile:
break
int
case
long
char
continue
return
default
short
do
sizeof
double
static
else
struct
switch
typedef
float
for
unsigned
goto
while