title: How to correctly allocate memory for an array of structs in C?
body: <p>This is the first time I'm using structs as I am still relatively new to C. When I try to input values for 3 structs the program crashes, but it works fine for 1 and 2. I think I may not actually be allocating enough memory. Is this the correct way to allocate memory for an array of structs?</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define MIN_SIZE 0
  #define MAX_SIZE 100
  #define MAX_MONTH_STR 9
  
  //Define a struct data type
  typedef struct
  {
      char* month;
      int day;
      int year;
  }date;
  
  //Method to allocate memory for a list of date structures
  date* allocateStruct(int size)
  {
      //Declaration of variables
      date *array;
      int i;
  
      //Allocate memory for rows (to store 'size' many pointers to 'date' struct data type)
      array = malloc(size*sizeof(date*));
  
      //For-loop to allocate memory for columns (to store 'size' many integers, and initialize them to zero)
      for (i=0; i&lt;size; i++)
      {
          array[i].month = calloc(MAX_MONTH_STR,sizeof(char));
          array[i].day = calloc(1,sizeof(int));
          array[i].year = calloc(1,sizeof(int));
      }
  
      return array;
  }
  
  //Method to free memory allocated
  //TO DO. . .
  
  int main()
  {
      //Declaration of variables
      int n;
      date* date_list;
      int i, j, k; //used in loops
  
      //Read input
      do
      {
          //printf("Enter number of dates you want to enter (between 1 and 10000):\n");
          scanf("%d", &amp;n);
      }while(n&lt;MIN_SIZE || n&gt;MAX_SIZE);
  
      //ALLOCATE MEMORY
      date_list = allocateStruct(n);
  
  
      //For-loop to store values in 'date_list'
      for (i=0; i&lt;n; i++)
      {
          //printf("Enter the date (month day year) in the following format: text number number");
          scanf("%s", date_list[i].month);
          scanf("%d", &amp;date_list[i].day);
          scanf("%d", &amp;date_list[i].year); //need &amp; ?
      }
  
  //--------&gt; crashes here if 3 dates are input, ok for 1 and 2
  
      //Test print
      for (i=0; i&lt;n; i++)
      {
          //printf("Enter the date (month day year) in the following format: text number number");
          printf("%s ", date_list[i].month);
          printf("%d ", date_list[i].day);
          printf("%d\n", date_list[i].year); //need &amp; ?
      }
  
      return 0;
  }
  </code></pre>