title: how to store strings inside an array in c
body: <p>How can I store a list of strings in an array in c. cause usually the a string e.g:'string' is stored as s|t|r|i|g in array(?). now if i have an array that has 10 index and want to store string in each one of those indexes, how do i do it? if not doable, what other data structure can i use for it.</p>
  
  <p>e.g: array = 'string1'|'string2'|..</p>
  
  <p>i have done something, but its not working: </p>
  
  <pre><code>// Array for name of alphabets, and name of states
   35     char *nameOfStates[numStates];
   36     char buffer[3];
   37 
   38     // Read the nameOfStates 
   39     int i;
   40     for(i=0;i&lt;numStates;i++){
   41         printf("Name of STATES:");
   42 
   43         int z=0;
   44         char *buffer2;
   45         while(z&lt;2 &amp;&amp; fgets(buffer,2,stdin) != NULL){
   46             
   47             buffer2 = strndup(buffer,2);
   48             z++;
   49         }// End while-loop
   50         nameOfStates[i] = buffer2;
   51     
   52     }// End of for-loop to read nameOfStates
  </code></pre>
  
  <p>EDIT: I realized that array[size] doesn't actually work! I did it cause of my java backgroun d and i thought it might work. so i changed the program but it still is throwing segmentation fault. I post the full(edited) program below:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;
  #include&lt;stdlib.h&gt;
  
  // Function declaration
  void analyze(char *string);
  void clearNewLines(void);
  
  
  int main(int argc, char *argv[]){
  
      // Number of states and number of alphabets of DFA
      int numStates;
      int numAlphabets;
  
  
      // Read numStates 
      printf("Enter the number of STATES:");
      scanf("%d",&amp;numStates);
  
      // Flush STDIN
      clearNewLines();
  
      // Array for name of alphabets, and name of states
      char **nameOfStates = malloc(numStates*sizeof(char*));
      char *buffer = NULL;    
      // Read the nameOfStates 
      int i;
      for(i=0;i&lt;numStates;i++){   
          printf("Name of STATES:");
  
          fgets(nameOfStates[i],2*sizeof(char),stdin);    
  
      }// End of for-loop to read nameOfStates
  
      clearNewLines();
      // Read numAlphabets
      printf("Enter the number of ALPHABETS: ");
      scanf("%d", &amp;numAlphabets);
  
      // Flush STDIN
      clearNewLines();
  
      // Array for name of alphabets, and name of states
      char nameOfAlphabets[numAlphabets]; 
      // Saving transition table
      char *transitionTable[numStates][numAlphabets];
  
  
      // Read name of alphabets
      int j;
      for(j=0;j&lt;numAlphabets;j++){
  
          printf("Name of ALPHABETS:");
          nameOfAlphabets[j] = getchar();
  
          // Flush STDIN 
          clearNewLines(); 
  
      }// End for-loop to read alphabets
  
      // Get the transitionTable[states][alphabets] 
      int row;
      for(row=0;row&lt;numStates;row++){
  
          int col;
          for(col=0;col&lt;numAlphabets;col++){
  
              printf("Enter Transition From %s to %c: ",nameOfStates[row],nameOfAlphabets[col]);
              printf("\n");
          }
  
      }
  
      return 0;
  }// End of main function
  
  /*
  *
  *   clearNewLines - clear any newline character present at the STDIN
  */
  void clearNewLines(void)
  {
      int c;
      do
      {
          c = getchar();
      } while (c != '\n' &amp;&amp; c != EOF);
  }
  </code></pre>