title: Dynamically Allocating a Matrix from Input - C
body: <p>I've been trying this code and it's not working out very well. </p>
  
  <pre><code>void *matrix_allocate_variable (int size)
  {
  void *p1;
  if ((p1=(int*)malloc(size))==NULL){
      printf("out of memory.\n");
      exit(1);
      }
  return p1;
  }
  </code></pre>
  
  <p>Here I created a function that call's <code>malloc</code> and exits upon error, so that I could use it in my next function:</p>
  
  <pre><code>void matrix_new(int **matrices, int *row_counts, int *column_counts, char specifier)
  {
  
  int index, i;
  index= (int)(specifier-'A');
  
  
      scanf("%d",&amp;row_counts[index]);
      scanf("%d",&amp;column_counts[index]);
  
      matrices[index]= (int*)matrix__allocate_variable(sizeof(int)*                                (row_counts[index]*column_counts[index]);
  </code></pre>
  
  <p>Here is where I am having problems. I'm trying to have the user enter some input for creating the matrix, but I'm having lots of problems trying to get this working. Can some one help me start this off?</p>
  
  <p>PS. For more details, I'm creating functions in <code>functions.c</code>, this are what I have so far. I have a <code>main.c</code> which calls these functions so that later on I can add, subract, and transpose, but as of now I am trying to enter in data which is what I'm having lot of trouble with.  Here is my <code>main.c</code> where I call the functions.   </p>
  
  <pre><code>/* Pointer to the set of matrix registers. */
  int **matrices = NULL;
  /* row_counts[i] stores the number of rows in matrix i */
  int *row_counts = NULL;
  /* column_counts[i] stores the number of columns in matrix i */
  int *column_counts = NULL;
  
  /**********************************************************************
  Skeleton code part B: suggested form for selected variable initializations
  **********************************************************************/
  /* Initialize the matrix set. */
  matrices = (int**) matrix_allocate_variable(...);
  column_counts = (int *)matrix_allocate_variable(...);
  row_counts = (int *)matrix_allocate_variable(...);
  char call[2];
  int error = 2;
  
  
  
           do {
              printf ( "&gt; ");
              if (scanf ("%1s", call) !=1) {
                      fprintf (stderr, "Command not found. \n");
                      exit (1);
              }
  
  
  switch (call [0]) {
  
                      case 'A':       matrix_new(matrices,row_counts,column_counts,'A');
                                              break;
                      case 'B':       matrix_new(matrices,row_counts,column_counts,'B');
                                              break;
                      case 'C':       matrix_new(matrices,row_counts,column_counts,'C');
                                              break;
                      case 'D':       matrix_new(matrices,row_counts,column_counts,'D');
                                              break;
                      case '+':       matrix_add(matrices,row_counts,column_counts);
                                              break;
                      case '^':       matrix_tranpose(matrices,row_counts,column_counts);
                                              break;
                      case '*':       matrix_multiply(matrices,row_counts,column_counts);
                                              break;
                      case '$':       exit (1);
  
                      default :       fprintf (stderr, "Command not found. \n");
  
              }
      } while (error != 1);
      return 0;
      }
  </code></pre>
  
  <p>Any help will be good and any pointers in what I should do next is great also. Thank you so so much every one.  </p>