title: Making 2D Array of Strings in C
body: <p>I am working on a project for my CS1 class and I have run into something I have never thought of before. I know at all arrays in C are essentially pointer to the first element of an array, and a string is really just a character array. However, for my assignment we have to read in a file, and part of the file is the following:</p>
  
  <pre>
  Brad Tim Rick (more man names separated by spaces)
  Lucy Angela Tina (more women names separated by spaces)
  </pre>
  
  <p>This is a short example, but what I have to do is extract the names and store them into two separate arrays, one for men and one for females. </p>
  
  <p>I have never worked with something like this, so of course I am confused. This is what I am trying to do, and of course its not working... oh yeah, and I'm trying to store them in dynamic allocation. The only spec says that the names will never exceed 19 characters (should I say twenty to allow the '/0' at the end of the string to still be there no matter what?) How can I tell the compiler, "hey I want an array of strings, and each string can hold 19 characters + 1 for the "string trailer '/0' "? And then how do I access those through pointers?</p>
  
  <pre><code>char **mens_names, **womens_names;
  
  mens_names = malloc(number_of_couples * sizeof(char[19]));
  womens_names = malloc(number_of_couples * sizeof(char[19]));
  
  if(mens_names == NULL){
  printf("Malloc failed! Memory could not be allocated to variable mens_names.");
  return -1;
  }
  
  int i;
  for(i = 0; i &lt; number_of_couples; i++){
      fscanf(input_file, "%s", &amp;mens_names[i]);
  }
  
  
  if(womens_names == NULL){
      printf("Malloc failed! Memory could not be allocated to variable womens_names.");
      return -1;
  }
  
  for(i = 0; i &lt; number_of_couples; i++){
      fscanf(input_file, "%s", &amp;womens_names[i]);
  }
  
  for(i = 0; i &lt; number_of_couples; i++){
      printf("Man: %s ", mens_names[i]);
      printf("Woman: %s\n", womens_names[i]);
  }
  </code></pre>