I have a function that dynamically creates a bisimentional array which memorizes a string of words read until "gata" is introduced.
The problem is that it crashes and I think the line
*(*words+*dim-1) = (char*)calloc(MAX_DIM,sizeof(char));
might be one of the problems.What is wrong with this line?
void read_words(char ***words,int *dim)
{
char buff[100];
*words = (char**)calloc(*dim,*dim*sizeof(char*));
while(strcmp(buff,"gata"))
{
printf("the new word : ");
scanf("%100s", buff);
if(strcmp(buff,"gata"))
{
dim++;
*words = (char**)realloc(words,*dim*sizeof(char*));
if(words == NULL)
{
printf("Memory allocation failed !\n");
exit(0);
}
*(*words+*dim-1) = (char*)calloc(MAX_DIM,sizeof(char));
strcpy(*(*words+*dim-1),buff);
}
}
}
int main()
{
char **words;
int i,dim = 0;
read_words(&words,&dim);
for (i = 0; i < dim; i++)
free(&words[i]);
free(words);
return 0;
}