title: segmentation fault in swap function
body: <p>im busy writing a line of code for my study. 
  I already have gotten quite far on the assignment but i keep running into the same problem. 
  On the swap function i keep running into a segmentation fault when a character is inputted(word &amp; word2) that is not in the main 'dictionary' string. 
  Could someone explain to me what is causing the problem and how i can solve it? Sorry if anything isnt clear, i've just started learning c++.</p>
  
  <p>code where segmentation fault occures:</p>
  
  <pre><code>  void swapWords(char **dict, char *word, char *word2)
  {
      int i; 
      int d;
      int x;
      int y;
      char *tmp;
  
  
      while (1){  
          for(i = 0; i &lt; MAX_NUMBER_OF_WORDS; i++)
          { 
              if(strcmp(word, dict[i]) != 0)
              {    
                  if(i == MAX_NUMBER_OF_WORDS -1)
                  {
                      printf("Cannot swap words. Atleast one word missing in the dictionary.\n");
                      goto error;
                  }
              }
              else
              {
                  x = i;
                  break;  
              }
  
          }
          for(d = 0; d &lt; MAX_NUMBER_OF_WORDS; d++)
          { 
              if(strcmp(word2, dict[d]) != 0)
              {    
                  if(d == MAX_NUMBER_OF_WORDS -1)
                  {
                      printf("Cannot swap words. Atleast one word missing in the dictionary.\n");
                      goto error;
                  }
              }
              else
              {
                  y = d;
                  break;
              }
  
          }
  
          tmp = dict[x];
          dict[x] = dict[y];
          dict[y] = tmp;
          error: break;
      }
  }
  </code></pre>
  
  <p>The entire code:</p>
  
  <pre><code>#define _CRT_SECURE_NO_WARNINGS
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;limits.h&gt;
  
  #define MAX_NUMBER_OF_WORDS 10
  
  void swapWords(char **dict, char *word, char *word2)
  {
      int i; 
      int d;
      int x;
      int y;
      char *tmp;
  
  
      while (1){  
          for(i = 0; i &lt; MAX_NUMBER_OF_WORDS; i++)
          { 
              if(strcmp(word, dict[i]) != 0)
              {    
                  if(i == MAX_NUMBER_OF_WORDS -1)
                  {
                      printf("Cannot swap words. Atleast one word missing in the dictionary.\n");
                      goto error;
                  }
              }
              else
              {
                  x = i;
                  break;  
              }
  
          }
          for(d = 0; d &lt; MAX_NUMBER_OF_WORDS; d++)
          { 
              if(strcmp(word2, dict[d]) != 0)
              {    
                  if(d == MAX_NUMBER_OF_WORDS -1)
                  {
                      printf("Cannot swap words. Atleast one word missing in the dictionary.\n");
                      goto error;
                  }
              }
              else
              {
                  y = d;
                  break;
              }
  
          }
  
          tmp = dict[x];
          dict[x] = dict[y];
          dict[y] = tmp;
          error: break;
      }
  }
  
  void removeWord(char **dict, char *word)
  {
      int i;
      int d;
      for(i = 0; i &lt; MAX_NUMBER_OF_WORDS; i++)
      { 
          if(strcmp(dict[i], word) == 0)
          {   dict[i] = NULL;
              for(d = i+1; d &lt; MAX_NUMBER_OF_WORDS; d++)
              { if(dict[d] == NULL)
                  { dict[i] = dict[d-1];
                      dict[d-1] = NULL;
                      break;
                  }
  
              }
              break;
          }
      }
  }
  
  void printDict(char **dict)
  {
      int i = 0;
  
      if(dict[0] == NULL)
      {
          printf("The dictionary is empty.\n");
      }
      else{
      while (dict[i] != NULL)
      {
          printf("- %s\n", dict[i]);
          i++;
      }
      }
  }
  
  void addWord(char **dict, char *word)
  {
      int d;
      char *word1;
  
              for(d = 0; d &lt; MAX_NUMBER_OF_WORDS; d++)
              {
                  if (dict[d] == NULL)
                  {           
                  word1 = (char*) malloc(sizeof(char)*(strlen(word) + 1));
                  strcpy(word1, word);
                  dict[d] = word1;
  
                  break;
                  }
              }
  }
  int numberOfWordsInDict(char **dict)
  {
      int i = 0;
      int d;
  
      for (d = 0; d &lt; MAX_NUMBER_OF_WORDS; d++){
      if(dict[d] != NULL)
      {
          i++;
      }
  }
  
      return i;
  }
  
  int main()
  {
      char *dict[MAX_NUMBER_OF_WORDS] = {};
      char word[36];
      char word2[36];
      char c;
      int i;
      while(printf("Command (a/p/r/s/q): "))
      {
      scanf(" %c", &amp;c);
      switch (c){
      case 'p':   printDict(dict);
                  break;
      case 'a':   printf("Enter a word: ");
                  scanf("%s", word);
                  addWord(dict, word);
                  break;
      case 'n':   i = numberOfWordsInDict(dict);
                  printf("%d\n", i);
                  break;
      case 'r':   printf("Remove a word: ");
                  scanf("%s", word);
                  removeWord(dict, word);
                  break;
      case 's':   printf("Swap two words:\n");
                  printf("Enter first word: ");
                  scanf("%s", word);
                  printf("Enter second word: ");
                  scanf("%s", word2);
                  swapWords(dict, word, word2);
                  break;
      case 'q':   return 0;
              }
          }
  }
  </code></pre>