title: Segmentation fault: 11 occurs when printing the contents of "Dictionary"
body: <p>I've been trying to create a dictionary of words and their definitions(sort of Oxford English Dictionary). So far, I've finished half of the job:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef struct{
  
      char wordInDictionary[32];
      int numberOfMeanings;
      char *wordDefinitions[10];
  
  }Entry; //struct that describes a single entry in the dictionary
  
  typedef struct{
  
      int entries;
      Entry* arrayOfEntries[100];
  
  }Dictionary; //dictionary itself
  
  Dictionary createDictionary(){
  
      Dictionary emptyDictionary;
  
      emptyDictionary.entries = 0;
  
      int i,j;
  
      for(i=0;i&lt;100;i++){
          emptyDictionary.arrayOfEntries[i] = malloc(sizeof(Entry));
          emptyDictionary.arrayOfEntries[i]-&gt;numberOfMeanings = 0;
          strcpy(emptyDictionary.arrayOfEntries[i]-&gt;wordInDictionary, "");
          for(j=0;j&lt;10;j++){
              emptyDictionary.arrayOfEntries[i]-&gt;wordDefinitions[j] = 
              malloc(500*sizeof(char));
  
              strcpy(emptyDictionary.arrayOfEntries[i]-&gt;wordDefinitions[j], "");
          }
  
      }
  
      return emptyDictionary; //created empty dictionary with 0 entries and empty content
  }
  
  Entry* searchWord(char word[], Dictionary *dict){
  
      Entry* foundWord;
  
      int i;
  
      for(i=0;i&lt;100;i++){
          if(strcmp(dict-&gt;arrayOfEntries[i]-&gt;wordInDictionary, word) != 0){
              foundWord = NULL;
          }else{
              foundWord = dict-&gt;arrayOfEntries[i];
              break;
          }
      }
  
      return foundWord; //searches for a given word and returns NULL, if word is not found, or pointer to entry in the dictionary that corresponds to word, if word is found.
  
  }
  
  int addDefinition(char word[], char meaning[], Dictionary *dict){
  
      Entry* foundEntry = searchWord(word, dict);
  
      int returnNumber;
  
      if((foundEntry == NULL || strcmp(foundEntry-&gt;wordInDictionary, word) != 0) 
              &amp;&amp; dict-&gt;entries &lt; sizeof(dict-&gt;arrayOfEntries)/sizeof(dict-&gt;arrayOfEntries[0])){
  
          Entry* newEntry = malloc(sizeof(Entry)); //allocating space if searched word is not found in dictionary
  
          strcpy(newEntry-&gt;wordInDictionary, word); //now there is a new word in the dictionary
          newEntry-&gt;numberOfMeanings = 0; //but the new word has 0 meanings
  
          dict-&gt;arrayOfEntries[dict-&gt;entries++] = newEntry;
  
          if(newEntry-&gt;numberOfMeanings&lt;10){ 
              newEntry-&gt;wordDefinitions[newEntry-&gt;numberOfMeanings] = malloc(500*sizeof(char));
              strcpy(newEntry-&gt;wordDefinitions[newEntry-&gt;numberOfMeanings], meaning); //new word now has a new meaning since its meanings' amount is less than 10(which is max)
          }
  
          int oldNumberOfMeanings = newEntry-&gt;numberOfMeanings;
          int newNumberOfMeanings = oldNumberOfMeanings + 1;
  
          if(newNumberOfMeanings&gt;oldNumberOfMeanings){
              returnNumber = 2; //returns 2 if both new entry and new definition were added into the dictionary
          }
  
          newEntry-&gt;numberOfMeanings++;
  
      }else if((foundEntry != NULL || strcmp(foundEntry-&gt;wordInDictionary, word) == 0) 
              &amp;&amp; dict-&gt;entries &lt; sizeof(dict-&gt;arrayOfEntries)/sizeof(dict-&gt;arrayOfEntries[0])){
  
          if(foundEntry-&gt;numberOfMeanings&lt;10){
              foundEntry-&gt;wordDefinitions[foundEntry-&gt;numberOfMeanings] = malloc(500*sizeof(char));
              strcpy(foundEntry-&gt;wordDefinitions[foundEntry-&gt;numberOfMeanings], meaning);
          }
  
          int oldNumberOfMeanings = foundEntry-&gt;numberOfMeanings;
          int newNumberOfMeanings = oldNumberOfMeanings + 1;
  
          if(newNumberOfMeanings == oldNumberOfMeanings){
              returnNumber = 0; //returns 0 if no new entry and no new meaning are added to already found one
          }else if(newNumberOfMeanings &gt; oldNumberOfMeanings){
              returnNumber = 1; //returns 1 if no new entry and one new meaning are added to already found one
          }
  
          foundEntry-&gt;numberOfMeanings++;
      }
  
      return returnNumber;
  }
  </code></pre>
  
  <p>So this vague piece of code describes my program. But the problem itself occurs when I test the code in <strong>main()</strong>:</p>
  
  <pre><code>int main(void){
  
      Entry* entryPtr;
  
      Dictionary dict = createDictionary();
  
      entryPtr = searchWord("include", &amp;dict);
  
      if(entryPtr != NULL){
          printf("\nWord found: 'include'");
      }else{
          printf("\nWord 'include' is not found in the dictionary.");
      }
  
      int count;
  
      count = addDefinition("include", "(verb) def1", &amp;dict);
      count = addDefinition("assist", "(verb) def2", &amp;dict);
      count = addDefinition("house", "(noun) def3", &amp;dict);
      count = addDefinition("camera", "(noun) def4", &amp;dict);
      count = addDefinition("valid", "(adjective) def5", &amp;dict);
  
      printf("\nLast count: %i", count);
  
      int i, j;
  
      for(i=0;i&lt;100;i++){
          if(strcmp(dict.arrayOfEntries[i]-&gt;wordInDictionary, "")!=0){
              printf("\n\t%i. %s", i+1, dict.arrayOfEntries[i]-&gt;wordInDictionary);
          }
          for(j=0;j&lt;10;j++){
              if(strcmp(dict.arrayOfEntries[i]-&gt;wordDefinitions[j], "")!=0){
                  printf("\n\t\t%i.%i. %s\n", i+1, j+1, dict.arrayOfEntries[i]-&gt;wordDefinitions[j]);
              }
          }
      }
  
      entryPtr = searchWord ("exam", &amp;dict);
      if (entryPtr != NULL) {
          printf("\nWord found: 'exam'");
      } else {
          printf("\nWord 'exam' is not found in the dictionary.");
      }
  
      entryPtr = searchWord ("include", &amp;dict);
      if (entryPtr != NULL) {
          printf("\nWord found: 'include'");
      } else {
          printf("\nWord 'include' is not found in the dictionary.");
      }
  
      count = addDefinition ("house", "(adjective) def6", &amp;dict);
      count = addDefinition ("house", "(adjective) def7", &amp;dict);
      count = addDefinition ("house", "(adjective) def8", &amp;dict);
  
      printf("\n\nThe the value returned by the last addition: is %i.", count);
  
  }
  </code></pre>
  
  <p>What the output gives is:</p>
  
  <pre><code>Word 'include' is not found in the dictionary.
  Last count: 2
          1. include
                  1.1. (verb) def1
  Segmentation fault: 11
  </code></pre>
  
  <p>I changed the condition in nested for loops in <strong>main()</strong> as follows:</p>
  
  <pre><code>for(i=0;i&lt;5;i++){
          if(strcmp(dict.arrayOfEntries[i]-&gt;wordInDictionary, "")!=0){
              printf("\n\t%i. %s", i+1, dict.arrayOfEntries[i]-&gt;wordInDictionary);
          }
          for(j=0;j&lt;1;j++){
              if(strcmp(dict.arrayOfEntries[i]-&gt;wordDefinitions[j], "")!=0){
                  printf("\n\t\t%i.%i. %s\n", i+1, j+1, dict.arrayOfEntries[i]-&gt;wordDefinitions[j]);
              }
          }
  }
  </code></pre>
  
  <p>Then it showed the right content, though it is not what it should be like. Instead, it should work for all the entries in the dictionary, not first 5.</p>
  
  <p>I've tried doing debugging many times and investigate it myself but still could not find the solution. Any hints to solve the possible problem(-s)?</p>
  
  <p>P.S. I run the program on VS Code, macOS, GCC.</p>