https://www.yogile.com/2et68g4vlfw/2...?vsc=NmY3MWIyM
Hey guys im having an error while running my code in c Eclipse.
so IN THE LINK THERE IS THE CODE WITH COULORS SO IT WILL BE EASY TO READ ....
ill be happy if you can look at the photo and tell me whats the problem with my code . thanks . btw sometime i also get this error
double free or corruption (out)
AND THATS THE FULL CODE :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void FreeBuffer();
void FreeBuffer() {
while (getchar() != '\n')
;
}
int main() {
int *size_of_def_for_each_word, Number_of_words, Number_of_def, i, j;
char ***Dictionary;
printf("Dear Sir or Miss, please Enter"
" Number of words you would like to add to your dictionary :\n");
while (1) {
if (scanf("%d", &Number_of_words) != 1) {
FreeBuffer();
printf("Incorrect Input\n");
printf("Dear Sir or Miss, please Enter Number of words "
"you would like to add to your dictionary: \n ");
} else {
break;
}
} //while(1)
printf("OK Sir So the numbers of words you chose to put in your "
"dictionary is: %d \n", Number_of_words);
//So until now we check the valid of user input and now will build the Dynamic Array.
size_of_def_for_each_word = (int*) malloc(Number_of_words * sizeof(int)); //here we Build Dynamic array that will keep the number of def in every word
// Dictionary = malloc(Number_of_words * sizeof(char**)); //here we Build the big array
for (i = 0; i < Number_of_words; i++) {
printf("How many Definition there for the %d word : \n ", i + 1);
while (1) {
if (scanf("%d", &Number_of_def) != 1) {
FreeBuffer();
printf("Incorrect Input\n");
printf(
"Dear Sir or Miss, please Enter Number of Definition there for the %d word \n",
i);
} else {
break;
}
} //while(1)
size_of_def_for_each_word[i] = Number_of_def;
printf("OK Sir So the numbers of Definition's you chose is: %d \n",
size_of_def_for_each_word[i]);
//on the next step for every word we will build Dynamic Array by number of definitions+ the word.
} //for num_of_words
/*
OK so until here size_of_def_for_each_word got basically number of definitions for each word
For example size_of_def_for_each_word[0]=3 > the first word got 3 expression .
*/
for (int i = 0; i < Number_of_words; i++) {
printf("%d ", size_of_def_for_each_word[i]);
printf("\n");
}
Dictionary = malloc(Number_of_words * sizeof(char **));
for (i = 0; i < Number_of_words; i++) {
Dictionary[i] = malloc((size_of_def_for_each_word[i]) * sizeof(char *));
for (j = 0; j < size_of_def_for_each_word[i]+1; j++) {
if (j == 0) { //here we enter the word in the array.
printf("please enter the %d word! : \n", i + 1);
FreeBuffer();
char input_Word[80];
Dictionary[i][j] = (char *) malloc((strlen(input_Word) + 1) * sizeof(char));
scanf("%s", Dictionary[i][j]);
printf(" Word number %d is : %s \n ", i + 1, Dictionary[i][j]);
}
else { //here we enter the definition of every word one by one .
printf("please enter the %d definition! : \n", j);
FreeBuffer();
char Input_Definition[200];
Dictionary[i][j] = (char *) malloc((strlen(Input_Definition) + 1) * sizeof(char));
scanf("%[^\t\n]s", Dictionary[i][j]);
printf(" Definition number %d is : %s \n ", j, Dictionary[i][j]);
}
}
}
for (i = 0; i < Number_of_words; i++) {
for (j = 0; j < size_of_def_for_each_word[i]+1; j++) {
if (j==0) {
printf("word is %s , it's definitions are ", Dictionary[i][j]);
free(Dictionary[i][j]);
} else {
printf("%s\t", Dictionary[i][j]);
free(Dictionary[i][j]);
}
}
free(Dictionary[i]);
printf("\n");
}
free(Dictionary);
printf("finished \n");
return 0;
} //Main